VueJS TypeError:警报不是函数

问题描述

我正在尝试学习VueJS。我已经做了第一个工作,正在测试指令。这是我的代码

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Partie 1 Chapitre 3 - Vue Mart</title>
</head>
<body>
<div id="app">
 
  <button v-on:click="alert('hello')">Cliquez ici !</button>

</div>

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
  const app = new Vue({
    el: '#app',data: {
      costOfApples: 5,costOfBananas: 2,costOfCoconuts: 8,favoriteColor: 'rose'
    },//Propriétés calculées
    computed: {
      totalAmount() {
        return this.costOfApples + this.costOfBananas + this.costOfCoconuts
      },label() {
        return ': '+ this.favoriteColor
      }
    }
  })
</script>

</body>
</html>

单击按钮时出现此错误

TypeError: alert is not a function
[Vue warn]: Error in v-on handler: "TypeError: alert is not a function"
[Vue warn]: Property or method "alert" is not defined on the instance but referenced during 
render. Make sure that this property is reactive,either in the data option,or for class-based components,by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

(found in <Root>)

我真的不知道从哪里开始寻找。对我来说,我的代码似乎还可以。 谢谢您的帮助

解决方法

您应该在方法中定义警报

methods(){
   alert(){
       // do something      
   }
}