如何解决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
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。