0

I am trying to understand how vue works. To do that I simplify it as much as possible, hence no webpack no CDNs or any other packages (unless its a necessity).

So, Came up with this but trying to inject a simple a variable into html gives vue is undefined error.

*vue.js file is taken from npm vue package.

<html>
<head>
  <script src="vue.js"></script>

  <script>
  new vue({
    data: 'This must be injected'
  }).$mount('#app');
  </script>

</head>
<body>
  <div id="app">
    <p> {{ data }} </p>
  </div>

  <h1>This is a sample header</h1>
</body>
</html>
1
  • 2
    new Vue (uppercase "V") Commented Dec 27, 2018 at 23:09

3 Answers 3

3

You need to have Vue load after the Dom is ready so it can find the element to attach itself to — don't put it in the head. Also it's upper-case Vue. And Data can't just be a naked property it should be a function that returns an object with your properties (it can also be an an object, but it's not recommended once the app starts getting bigger).

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">

  <p> {{ data }} </p>

</div>

<h1>This is a sample header</h1>
<script>
  Vue.config.productionTip = false
  new Vue({
    data(){ return {
      data: 'This must be injected'
    }
  }
  }).$mount('#app');
</script>

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

Comments

1

There are a few problems in your code.

  1. vue should be Vue
  2. data should be an Object or Function - https://v2.vuejs.org/v2/api/#data

<html>
<head>  
</head>

<body>
  <div id="app">

    <p> {{ myText }} </p>

  </div>

  <h1>This is a sample header</h1>

  <script src="vue.js"></script>

  <script>
    new Vue({
      data: {
        myText: 'This must be injected'
      }
    }).$mount('#app');

  </script>

</body>
</html>

2 Comments

this will not work since you're placing that script inside head element
@BoussadjraBrahim Fixed. Thanks
0

I think the data must have an object as it's value.

<html>
  <head>
  <script src="vue.js"></script>

<script>

new Vue({
  data: {
     txt: 'This must be injected'
  }
}).$mount('#app');

</script>

  </head>
  <body>
  <div id="app">

  <p> {{ txt }} </p>

  </div>

  <h1>This is a sample header</h1>
  </body>
</html>

Comments

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.