VUE学习-mixin混入
2024-10-21 13:20:41
mixin混入
混入 (mixin) 提供了一种非常灵活的方式,来分发 Vue 组件中的可复用功能。
组件式混入
// 定义一个混入对象
var myMixin = {
created: function () {this.hello()},
methods: {
hello: function () {
console.log('hello from mixin!')
}
}
}
// 定义一个使用混入对象的组件
var Component = Vue.extend({mixins: [myMixin]})
var component = new Component() // => "hello from mixin!"
vue实例混入
数据对象在内部会进行递归合并,并在发生冲突时以组件数据优先。
var mixin = {
data: function () {
return {message: 'hello',foo: 'abc'}
}
}
new Vue({
mixins: [mixin],
data: function () {
return {message: 'goodbye',bar: 'def' }
},
created: function () {
console.log(this.$data)
}
// => { message: "goodbye", foo: "abc", bar: "def" }
})
同名钩子函数将合并为一个数组,因此都将被调用。另外,混入对象的钩子将在组件自身钩子之前调用。
var mixin = {
created: function () {
console.log('混入对象的钩子被调用')
}
}
new Vue({
mixins: [mixin],
created: function () {
console.log('组件钩子被调用')
}
})
// => "混入对象的钩子被调用"
// => "组件钩子被调用"
值为对象的选项,例如 methods、components 和 directives,将被合并为同一个对象。两个对象键名冲突时,取组件对象的键值对。
var mixin = {
methods: {
foo: function () {
console.log('foo')
},
conflicting: function () {
console.log('from mixin')
}
}
}
var vm = new Vue({
mixins: [mixin],
methods: {
conflicting: function () {
console.log('from self')
}
}
})
vm.foo() // => "foo"
vm.conflicting() // => "from self"
全局混入
一旦使用全局混入,它将影响每一个之后创建的 Vue 实例。// 为自定义的选项 'myOption' 注入一个处理器。
Vue.mixin({
created: function () {
var myOption = this.$options.myOption;
if (myOption) {console.log(myOption)}
}
})
new Vue({myOption: 'hello!'})
// => "hello!"
自定义选项合并策略
自定义选项将使用默认策略,即简单地覆盖已有值。如果想让自定义选项以自定义逻辑合并,可以向 Vue.config.optionMergeStrategies
添加一个函数。
Vue.config.optionMergeStrategies.myOption = function (toVal, fromVal) {
/* 返回合并后的值 */
}
// Vuex 1.x 的混入策略
const merge = Vue.config.optionMergeStrategies.computed
Vue.config.optionMergeStrategies.vuex = function (toVal, fromVal) {
if (!toVal) return fromVal
if (!fromVal) return toVal
return {
getters: merge(toVal.getters, fromVal.getters),
state: merge(toVal.state, fromVal.state),
actions: merge(toVal.actions, fromVal.actions)
}
}
页面混入模块
var mixinClock = {
data() {
return {
clock: '',
interalId: '',
}
},
created() {
this.interalId = setInterval(()=>{
this.setClock()
}, 1000)
},
destroyed() {
clearInterval(this.interalId)
},
methods:{
setClock() {
this.clock = this.dateTimeFormatter(new Date())
console.log(this.clock)
},
dateTimeFormatter(_date) {
let date = new Date(_date);
let y = date.getFullYear();
let M = (date.getMonth()).toString().padStart(2, '0');
let d = (date.getDate()).toString().padStart(2, '0');
let h = (date.getHours()).toString().padStart(2, '0');
let m = (date.getMinutes()).toString().padStart(2, '0');
let s = (date.getSeconds()).toString().padStart(2, '0');
return `${y}-${M}-${d} ${h}:${m}:${s}`
}
}
}
new Vue({
el: '.main-container',
mixins: [mixinClock],
})
最新文章
- JavaScript之周道长浅谈变量使用中的坑
- 【工具相关】iOS-Reveal的使用
- 【POI xls Java map】使用POI处理xls 抽取出异常信息 --java1.8Group by ---map迭代 -- 设置单元格高度
- function设置jsp页面使用js控制文本框只读,并且按下backspace删除按钮后停在原页面
- SQL IDENTITY(int,1,1) 用法
- Nginx开发从入门到精通
- 10.21_Nutz批量插入顺序,POI,wiki持续关注,POI,SSH,数据库优先
- 熄灯问题 --POJ 2811-ACM
- 动态规划晋级——POJ 3254 Corn Fields【状压DP】
- UML和模式
- Paxos 实现日志复制同步(Multi-Paxos)
- 数组的map方法
- JavaScript代码规范和性能整理
- 我的Android之路——底部菜单栏的实现
- 导出oracle序列
- spring boot log4j2与三方依赖库log4j冲突无法初始化问题解决方法
- 移动键盘 滚动input
- 一个linux下简单的纯C++实现Http请求类(GET,POST,上传,下载)
- Centos上执行Shell的四种方式
- VS2010/MFC编程入门之四十四(MFC常用类:定时器Timer)
热门文章
- Hello 2023 A-D
- Nacos详解
- 建立一个简单干净的 gn+ninja 工具链
- 在 K8S Volume 中使用 subPath
- Java学习笔记:2022年1月13日(其一)
- Codeforces Round #569 (Div. 2)
- html内容超宽后,缩小可视区域后,会引起部分背景色宽度出现显示异常情况,解决如下:
- 解决angular11打包报错Type 'Event' is missing the following properties from type 'any[]': ...Type 'Event' is not assignable to type 'string'
- Lock锁-线程状态概述
- linux基础(部分讲解)