1

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?

3
  • 4
    Well, one, your script to create the Vue executes before #filter exists. Commented Jan 5, 2018 at 16:13
  • @Bert as in the order of the script tag? I moved the script tag below anchor with same result. Console does not log. Commented Jan 5, 2018 at 16:15
  • Wrap the A in a div with ID filter and then remove the filter ID from anchor tag Commented Jan 5, 2018 at 16:16

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Isn't vue technically babel? That's what this led me to believe vue-loader.vuejs.org/en/features/es2015.html so why does using text/babel break it?
babel is a transpiler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.