vue之自定义插件

rulesPlugin.js

const RulesPlugin = {
  install (Vue) {
    Vue.mixin({
      created() {
        const rules = this.$options.rules
        if (rules) {
          Object.keys(rules).forEach(key => {
            const {validate, message} = rules[key]
            this.$watch(key, newValue => {
              const valid = validate(newValue)
              if (!valid){
                console.error(message)
              }
            })
          })
        }
      },
    })
  }
}



  
  
  
  自定义插件
  
  


  

 

你可能感兴趣的:(Vue)