【问题标题】:Method can not be found when using v-on Instance Method使用 v-on Instance Method 时找不到方法
【发布时间】:2015-11-24 07:59:10
【问题描述】:

我在 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 模板中触发点击? 谢谢。

【问题讨论】:

    标签: vue.js


    【解决方案1】:

    基于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
          })
        },
      }
    })
    

    【讨论】:

    • 感谢您花时间发布解决方案,而不是立即关闭答案,我一直在为此绞尽脑汁。 :)
    猜你喜欢
    • 1970-01-01
    • 2019-07-02
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2013-05-25
    • 1970-01-01
    相关资源
    最近更新 更多