全局自定义组件
Vue.component('component-name',{
data(){
return{
count:0
}
},
template:
`<button @cick="addCount">{{count}}</button>`,
methods:{
addCount(){
this.count++
}
}
})
- vue-cli3.0中可能会报错。在vue.config.js中设置。
module.exports = {
runtimeCompiler:true
}
- 为什么data不对象,而选择用一个闭包函数。
- 因为要保证每个引用data中数值的内容都能给出一份新的拷贝,如果是一个对象,
- count和count之间会互相影响。
全局注册指令
Vue.directive('watch-color',{
bind(el
,bind){
},
update(el,bind){
console.log(bind.value)
var backgroundColor = bind.value.backgroundColor;
var color = bind.value.color;
el.style.color = color;
el.style.backgroundColor = backgroundColor;
},
inserted(el,bind){
},
unbind(el,bind){}
})
<div v-watch-color="{backgroundColor:red,color:white}"></div>