VueJS:scope.notifications不是一个函数



我在Vue.js 1.0.21中定义了一个方法,在尝试使用它时遇到了以下错误。我使用的是CLI版本,所以语法与我习惯的有点不同。

错误

TypeError: scope.notifications is not a function. (In 'scope.notifications()', 'scope.notifications' is undefined)

Navbar.vue

<template>
  <div id="navbar">
    <!-- ... -->
    <a href="#data" @click.prevent="notifications()"><i class="fa fa-bell-o"></i></a>
    <!-- ... -->
  </div> <!-- /navbar -->
</template>
<script>
export default {
  data () {
    return {
      versionIsVisible: false,
      version: '2.0.0'
    };
  },
  methods () {
    return {
      notifications: function () {
        console.log('Notifications');
      }
    };
  }
};
</script>
<style lang="scss" scoped>
@import '../assets/sass/settings';
// ...
</style>

methods不应该是返回对象的函数。它应该是文档中所述的对象。

<script>
  export default {
    methods: {
      notifications: function () {
        console.log('Notifications');
      }
    }
  };
</script>
methods () {
  return {
    notifications: function () {
      console.log('Notifications');
    }
  };
}

应该是函数的对象,而不是函数:

methods: {
  notifications: function () {
    console.log('Notifications');
  }
}

最新更新