全局函数回调

全局函数可以写一个文件globalFunc.js

exports.install = function(Vue, option){
Vue.prototype.setData = function(that, key){
that[key] = '222'
}

Vue.prototype.testCallMe = function(str){
console.log('test call me' + str)
}

Vue.prototype.testCallBack = function(func, param){
func(param)
this.testCallMe('tetetet')
}
}
main.js

import globalFunc from '@/components/globalFunc'

Vue.use(globalFunc)

vue文件中

调用

this.testCallBack(this.test, 'yui0')//使用全局函数调用vue文件中的函数,修改vue文件中的数据

this.setData(this, 'msg')//使用全局函数修改vue文件中的数据

test函数编写

test:function(str){
this.msg = '233' + str
},

原文地址:https://www.cnblogs.com/dianzan/p/15480920.html