美文网首页
怎么给vue定义全局的方法?

怎么给vue定义全局的方法?

作者: 死敲代码的 | 来源:发表于2020-01-03 13:16 被阅读0次
1. prototype
Vue.prototype.testFn = function(){
    console.log("test")
}
2. mixin
Vue.mixin({
  created: function () {
      console.log("test")
  }
})
3.$root
// 创建全局方法
this.$root.$on('test', function(){
    console.log('test');
})
// 销毁全局方法
this.$root.$off('test');
// 调用全局方法
this.$root.$emit('test');

相关文章

网友评论

      本文标题:怎么给vue定义全局的方法?

      本文链接:https://www.haomeiwen.com/subject/huezoctx.html