如何修复Vue JS警报

时间:2018-12-20 15:48:07

标签: javascript html vue.js vuejs2

我有一个非常简单的index.html文件,用于学习Vue。我已经在按钮上附加了click事件,应该可以提醒用户。

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参数,但这无济于事。有什么我想念的吗?

1 个答案:

答案 0 :(得分:3)

您的Vue应用已绑定到#app

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

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