Vue partial filter and local custom instruction

  • Whether local custom command or the local filter can only be used within the current component, from the invalid current component
// 全局自定义指令可以在任何组件中使用
        Vue.directive('mycolor', {
          inserted(el, binding) {
            console.log(binding);
            // binding.value可以获取传入自定义指令中的属性的值
            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
          }
        }
      })
// 局部自定义指令通过在组件内部使用directives属性创建
      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()
            }
          }
        }
      })

Guess you like

Origin blog.csdn.net/weixin_42442123/article/details/93708884