javascript - 我如何修复 Vue JS 警报

标签 javascript html vue.js vuejs2

我有一个非常简单的 index.html 文件,我用它来学习 Vue。我在按钮上附加了一个点击事件,它应该提醒用户。

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Hello Visitor</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" media="screen" href="main.css" />
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

</head>
<body>

    <div class="container">
        <div id="app">
            {{ message }}
        </div>
            <input type="text"> </br> </br>
            <button v-on:click="greet" class="btn btn-primary">Greet Me!</button>   
    </div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src="main.js"></script>
</body>
</html>

main.js

new Vue({
   el: '#app',
   data: {
       message: 'What is your name?'
    },
    methods: {
       greet: function(){
          alert("Hello to you too.");
       },
    }
});

我认为该函数需要函数中的 event 参数,但这没有帮助。有什么我想念的吗?

最佳答案

你的 Vue 应用绑定(bind)到 #app .

在 div 之外发生的事情,Vue 不会解析或知道。

将您的按钮移到 #app 中div,它应该起作用。您可能还想提供您的 <button>一个type="button" - 这将阻止它尝试提交表单,这是默认行为。 (您也可以执行 <button @click.prevent="greet"> 来完成同样的事情。.prevent 会阻止默认行为。)

关于javascript - 我如何修复 Vue JS 警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53871905/

相关文章:

javascript - 如何以屏幕阅读器仍会阅读文本并向用户指示它已被禁用的方式将单选按钮设置为 "disable"?

javascript - jQuery 自动完成最小长度

javascript - 单堆叠条形图数据设置?

javascript - vuejs MaterializeCSS timepicker 没有获取值

javascript - 将 Javascript 变量传递给 grails RemoteFunction

jQuery 对话框未捕获控件 - IE 9.0

javascript - insertRow() 不适用于 FOR 循环

javascript - 在 div 内显示 JSON 内容

javascript - 使用 vue js 和 axios 下载时 XLSX 文件损坏

vue.js - *Nuxt JS Auth* 为什么在模板部分使用 loginWith 成功登录后 auth.loggedIn 为真,但在中间件文件中为假?