1
votes

I'm trying to create a click event in Vue in a static HTML file as test but it doesn't work as expected. See full code below:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--[if IE]><![endif]-->
<!--Empty IE conditional comment to improve site performance-->
<html lang="en">

<head>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>

<script type="text/babel">
var titleFilter = new Vue({
  el: '#filter',
  methods: {
    clickEvent: function(){
        console.log('Vue is working.');
    }
  }
});
</script>

<a id="filter" v-on:click="clickEvent" href="#">CLICK ME</a>

</body>
</html>

So as a checklist:

  1. I have my function in a method object
  2. Function names are correct
  3. I'm including the library
  4. In my script tag, I'm specifing babel as type

It's not working, what's wrong?

1
Well, one, your script to create the Vue executes before #filter exists. - Bert
@Bert as in the order of the script tag? I moved the script tag below anchor with same result. Console does not log. - kawnah
Wrap the A in a div with ID filter and then remove the filter ID from anchor tag - Derek Pollard

1 Answers

3
votes

This works

<html lang="en">

<head>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>

</head>
<body>

<a id="filter" v-on:click="clickEvent" href="#">CLICK ME</a>
<script type="text/javascript">
var titleFilter = new Vue({
  el: '#filter',
  methods: {
    clickEvent: function(){
        console.log('Vue is working.');
    }
  }
});
</script>
</body>
</html>

Change text/babel to text/javascript and move your JS code below