- 不管是局部自定义指令还是局部过滤器都只能在当前组件内使用,脱离当前组件无效
Vue.directive('mycolor', {
inserted(el, binding) {
console.log(binding);
el.style.color = binding.value
}
})
Vue.component('one', {
data () {
return {
time: new Date(),
color: 'red'
}
},
template: `
<div>
<p>{{time | fmtTime}}</p>
<input type="text" v-mycolor="color">
</div>
`,
filters: {
fmtTime(time) {
console.log(time);
var y = time.getFullYear();
var m = time.getMonth() + 1;
var d = time.getDate();
return y + '/' + m + '/' + d
}
}
})
Vue.component('two', {
data () {
return {
time: new Date(),
color: 'red'
}
},
template: `
<div>
<p>{{time}}</p>
<input type="text" v-myfocus v-mycolor="color">
</div>
`,
directives: {
myfocus: {
inserted(el, binding) {
console.log(el);
console.log(binding);
el.focus()
}
}
}
})