admin管理员组

文章数量:1313073

I try to get into vue.js and I'm stuck.

Html page:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src=""></script>
    <script src="test.js"></script>
</head>
<body>
    <div id="app">
        <button v-on:click="exampleFunction">General</button>
    </div>
</body>
</html>

test.js

var app = new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue!'
  },
   created: function () {
       console.log('Vue instance was created');
   },
   methods:  {
       exampleFunction: function () {
           console.log('This is an example function');
       }
   },
   destroyed: function () {
       console.log('Vue instance was destroyed');
   }
})

app.exampleFunction();

Console output:

Vue instance was created
This is an example function

The problem is that the button does not work, it's writing nothing on the console when I click.

Any suggestion?

I try to get into vue.js and I'm stuck.

Html page:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr/npm/vue"></script>
    <script src="test.js"></script>
</head>
<body>
    <div id="app">
        <button v-on:click="exampleFunction">General</button>
    </div>
</body>
</html>

test.js

var app = new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue!'
  },
   created: function () {
       console.log('Vue instance was created');
   },
   methods:  {
       exampleFunction: function () {
           console.log('This is an example function');
       }
   },
   destroyed: function () {
       console.log('Vue instance was destroyed');
   }
})

app.exampleFunction();

Console output:

Vue instance was created
This is an example function

The problem is that the button does not work, it's writing nothing on the console when I click.

Any suggestion?

Share Improve this question asked Feb 16, 2018 at 10:51 B. BriB. Bri 5853 gold badges7 silver badges26 bronze badges 4
  • Actually in your console output I see "This is an example function", that's the log printed out by exampleFunction, that's bound to the :click directive. – P3trur0 Commented Feb 16, 2018 at 11:14
  • Your code seems to be working fine to me. But, try replacing it with: <button @click="exampleFunction()">General</button> – haMzox Commented Feb 16, 2018 at 11:31
  • The console output contains only the result of the call from the js itself (declared after the Vue object), it should add a line each time I click. – B. Bri Commented Feb 16, 2018 at 12:26
  • Still no movement on console with @click. – B. Bri Commented Feb 16, 2018 at 12:32
Add a ment  | 

3 Answers 3

Reset to default 6

It works fine in a snippet. You might want to wrap your Vue call in DOMContentLoaded to ensure the DOM is there before Vue tries to attach to it, as I did below.

document.addEventListener("DOMContentLoaded", function(event) {
  var app = new Vue({
    el: '#app',
    data: {
      message: 'Hello Vue!'
    },
    created: function() {
      console.log('Vue instance was created');
    },
    methods: {
      exampleFunction: function() {
        console.log('This is an example function');
      }
    },
    destroyed: function() {
      console.log('Vue instance was destroyed');
    }
  })

  app.exampleFunction();
});
<script src="https://cdn.jsdelivr/npm/vue"></script>
<div id="app">
  <button v-on:click="exampleFunction">General</button>
</div>

The problem is that you are loading your test.js script before the DOM elements are created on the page. In other words, the script is executing on the page before anything is created.

While the DOMContentLoaded is one way to solve the problem, I would remend moving your script tags to the bottom of the body element. Remember that <script> tags are render blocking and are usually offloaded to the bottom of the <body> element to improve performance as well. By doing so, it should also resolve your issue as well.

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div id="app">
        <button v-on:click="exampleFunction">General</button>
    </div>
    <script src="https://cdn.jsdelivr/npm/vue"></script>
    <script src="test.js"></script>
</body>
</html>

You can see my example here https://codepen.io/BenCodeZen/project/editor/XWMNNg

There is nothing wrong with your code. That code is working absolutely fine in my browser.

Delete your browser's cache and try again

本文标签: javascriptvonclick does not work (vuejs)Stack Overflow