我在 js 模板(稍后在组件中使用)中使用 v-on
来触发点击事件:
v-on='click : favoritesToggle(venue.slug, $event)'
但是当我点击它时,我得到了
Uncaught TypeError: scope.favoritesToggle is not a function
vue 对象仍然在控制台中找到它:
vm.favoritesToggle
> exports.bind(a)
我在同一页面上还有一些其他的 v-on
单击事件并且运行良好,但它们不在 js 模板中,这可能与找不到方法有关,因为我是在js模板中点击触发?
谢谢。
最佳答案
基于 docs我理解在组件内调用的方法应该放在该组件内。
示例:
Vue.component('venue-component', {
template: $('#venue-template').html(),
methods: {
favoritesToggle: function (slug, e) {
e.preventDefault()
var resource = this.$resource('/api/venues/' + slug + '/favorites_toggle')
resource.save({
method: 'like' // unlike
}, function (data) {})
.success(function (data, status, request) {
// handle success
}).error(function (data, status, request) {
// handle error
})
},
}
})
关于vue.js - 使用 v-on Instance Method 时找不到方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32291675/