如题所述
methodsä¸çfunctionä¸çthisæåvueå®ä¾ï¼å
¶ä»ç没ä»ä¹è¿ç§è°ç¨æ¹å¼æ¯ç´æ¥è®¿é®test2å½æ°ï¼æ²¡æä»»ä½çthisç»å®ï¼æ以è¯å®è®¿é®ä¸å°
this.$options.methods.test2();
èç´æ¥è°ç¨this.test2()ï¼å é¨è¯å®åäºthisç»å®çï¼ä¾å¦
this.$options.methods.test2.bind(this)();
æ´æ°ï¼Vueæºç ä¸çå¤ç
/**
* Setup instance methods. Methods must be bound to the
* instance since they might be passed down as a prop to
* child components.
*/
Vue.prototype._initMethods = function () {
var methods = this.$options.methods
if (methods) {
for (var key in methods) {
this[key] = bind(methods[key], this)
}
}
}
function bind (fn, ctx) {
return function (a) {
var l = arguments.length
return l
? l > 1
? fn.apply(ctx, arguments)
: fn.call(ctx, a)
: fn.call(ctx)
}
}
this.$options.methods.test2();
èç´æ¥è°ç¨this.test2()ï¼å é¨è¯å®åäºthisç»å®çï¼ä¾å¦
this.$options.methods.test2.bind(this)();
æ´æ°ï¼Vueæºç ä¸çå¤ç
/**
* Setup instance methods. Methods must be bound to the
* instance since they might be passed down as a prop to
* child components.
*/
Vue.prototype._initMethods = function () {
var methods = this.$options.methods
if (methods) {
for (var key in methods) {
this[key] = bind(methods[key], this)
}
}
}
function bind (fn, ctx) {
return function (a) {
var l = arguments.length
return l
? l > 1
? fn.apply(ctx, arguments)
: fn.call(ctx, a)
: fn.call(ctx)
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2017-08-04
methods中的function中的this指向vue实例,其他的没什么这种调用方式是直接访问test2函数,没有任何的this绑定,所以肯定访问不到
第2个回答 2017-08-04
methods中的function中的this指向vue实例,其他的没什么这种调用方式是直接访问test2函数,没有任何的this绑定,所以肯定访问不到