【问题标题】:How do I fix Vue JS alert如何修复 Vue JS 警报
【发布时间】:2018-12-20 15:48:06
【问题描述】:

我有一个非常简单的 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 应用绑定到#app。发生在该 div 之外的东西,Vue 不会解析或知道。
  • 您的按钮不在您的el...

标签: javascript html vue.js vuejs2


【解决方案1】:

您的 Vue 应用程序已绑定到 #app

发生在该 div 之外的东西,Vue 不会解析或知道。

将您的按钮移动到 #app div 内,它应该可以正常工作。您可能还想给您的&lt;button&gt; 一个type="button" - 这将阻止它尝试提交表单,这是默认行为。 (您也可以使用&lt;button @click.prevent="greet"&gt; 来完成相同的操作。.prevent 会阻止默认行为。)

【讨论】:

  • 哇。我在那里完全失明。我一定会添加type=button。谢谢一百万!
猜你喜欢
  • 2019-09-21
  • 2022-01-09
  • 2019-11-27
  • 2022-11-24
  • 2021-05-20
  • 1970-01-01
  • 1970-01-01
  • 2019-03-03
  • 1970-01-01
相关资源
最近更新 更多