VueJS:scope.notifications不是一个函数

时间:2016-09-16 14:28:13

标签: vue.js

我在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>

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:3)

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

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

methods: {
  notifications: function () {
    console.log('Notifications');
  }
}
相关问题