1

I'm using Vue.js for the first time and can't get the simplest code snippet to work. I'm currently adding it to an existing project using Webpack and this is what the code looks like in my js file:

import Vue from 'vue'
import 'bootstrap';
...

var app = new Vue({
  el: '#toto',
  data: {
    message: 'Hello Vue!'
  }
});

And my HTML is quite long but this is what I'm trying to do:

<div id="main">
  <div class="container">
    <div id="toto">{{ message }}</div>
    <h2 class="title">Search results</h2>
  </div>
</div>

So when I run the js code above, the "toto" div is empty. I'm not getting any compilation error in Webpack and no error in the Chrome console either.

What am I doing wrong?

8
  • Does your JS execute at all? Try adding some console.log('HERE)` at the top of the file Commented Jun 16, 2020 at 9:40
  • @BroiSatse All JS code besides the Vue part. Commented Jun 16, 2020 at 9:48
  • I'm not sure what your answer mean, but I'll assume it was printed out in the console? Is the code executing when element #toto has been loaded, i.e is the script loaded at the end of your html? Commented Jun 16, 2020 at 10:05
  • @BroiSatse I think I replied before you edited the original message. So to answer your question, all JS code works besides the Vue instantiation (I even added the log messages before and after the Vue part). Now oddly enough, when I change the "el" attribute to "#main", the whole main div is emptied. Does it help? Commented Jun 16, 2020 at 10:30
  • 1
    So the link above solved the issue. And I had to remove the "template" attribute for it to work. Commented Jun 16, 2020 at 12:45

2 Answers 2

1

I found the solution here: Vue replaces HTML with comment when compiling with webpack

The following code added to the Webpack configuration file did the trick:

module.exports = {
  // ...
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

-1

When you use Webpack Vue usually expects Single-File components (*.vue) - you can not put a template inside your public HTML file. So you have to do something like this:

var app = new Vue({
  el: '#toto',
  template: '{{ message }}',
  data: {
    message: 'Hello Vue!'
  }
});

5 Comments

I don't get it, but I tried your code and it didn't change anything. Can you elaborate on the new file structure and changes please? What does the "template" attribute do?
Check out this JSfiddle jsfiddle.net/pc5Lubvd
Isn't that my current code with the "el" attribute changed from "toto" to "main"?
@IVOGELOV - What's interesting is that your fiddle do not have template listed and it still works.
The point is that if you use Webpack (and thus Single-File components in *.vue files) - you do not need the template property. If you are loading Vue at runtime on a plain HTML page - then you have to either provide the template as string or the ID of <template> tag in your HTML.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.