1. 程式人生 > >vue在全域性函式中加回調,呼叫vue檔案中的函式

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
},