2016-09-16 32 views
1

我已经在Vue.js 1.0.21中定义了一个方法,并且在尝试使用它时收到以下错误。 我正在使用CLI版本,因此语法与我习惯的语法稍有不同。VueJS:scope.notifications不是函数

错误

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

methods不应该返回一个对象的功能。它应该是docs中所述的对象。

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

应的功能的对象,而不是函数:

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