vue在全局函数中加回调,调用vue文件中的函数

全局函数可以写一个文件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
}, 

猜你喜欢

转载自blog.csdn.net/youyudexiaowangzi/article/details/82261672