const myMixin = {
created() {
this.hello()
},
methods: {
hello() {
console.log('hello from mixin!')
}
}
}
const Component = Vue.extend({
mixins: [myMixin]
})
const mixin = {
data() {
return {
message: 'hello',
foo: 'abc'
}
}
}
new Vue({
mixins: [mixin],
data() {
return {
message: 'goodbye',
bar: 'def'
}
},
created() {
console.log(this.$data)
}
})
Vue.mixin({
created() {
const myOption = this.$options.myOption
if (myOption) {
console.log(myOption)
}
}
})
Vue.directive('focus', {
inserted(el) {
el.focus()
}
})
directives: {
focus: {
inserted(el) {
el.focus()
}
}
}
Vue.directive('demo', {
bind(el, binding, vnode) {
},
inserted(el, binding, vnode) {
},
update(el, binding, vnode, oldVnode) {
},
componentUpdated(el, binding, vnode, oldVnode) {
},
unbind(el, binding, vnode) {
}
})
Vue.directive('demo', {
bind(el, binding, vnode) {
console.log(binding.value)
console.log(binding.oldValue)
console.log(binding.arg)
console.log(binding.modifiers)
}
})
Vue.directive('permission', {
inserted(el, binding) {
const { value } = binding
const permissions = store.getters.permissions
if (value && !permissions.includes(value)) {
el.parentNode && el.parentNode.removeChild(el)
}
}
})
<button v-permission="'admin'">删除</button>
Vue.directive('debounce', {
inserted(el, binding) {
let timer
el.addEventListener('click', () => {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
binding.value()
}, 1000)
})
}
})
<button v-debounce="handleClick">提交</button>
Vue.directive('lazy', {
inserted(el, binding) {
const observer = new IntersectionObserver(([entry]) => {
if (entry.isIntersecting) {
el.src = binding.value
observer.unobserve(el)
}
})
observer.observe(el)
}
})
<img v-lazy="imageUrl" />