javascript - vue这里的this指向哪里?
本文介绍了javascript - vue这里的this指向哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
var app5 = new Vue({
el: '#app-5', data: {message: 'Hello Vue.js!'}, methods: {
reverseMessage: function () {
this.message = this.message.split('').reverse().join('')
}
}
})
解决方案
Methods中的方法中的this会被vue自己绑定到当前vm实例上。
直接看源码吧
https://github.com/vuejs/vue/...
function initMethods (vm: Component) {
const methods = vm.$options.methods
if (methods) {
for (const key in methods) {
vm[key] = methods[key] == null ? noop : bind(methods[key], vm)
if (process.env.NODE_ENV !== 'production' && methods[key] == null) {
warn(
`method "${key}" has an undefined value in the component definition. ` +
`Did you reference the function correctly?`,
vm
)
}
}
}
}
这篇关于javascript - vue这里的this指向哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文