0

I have a really simple code. I'm trying to learn Vue by following this tutorial on youtube https://www.youtube.com/watch?v=4deVCNJq3qc

I am stuck at 31:30 with this code:

Vue.component('car-list', {
 template:
   <ul>
    <li v-for="car in cars">{{ car }}</li>
   </ul>
 })

for some reason it breaks the whole code and when i inspect the page on google chrome i get the error

Uncaught SyntaxError: Unexpected token '<'

screenshot of the error: https://ibb.co/CnzRFtZ

I am using Atom as my editor.

my whole code

<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.18/vue.min.js"></script>
  </head>
  <body>
    <div id="root">
      {{ carzName }}
      <br>
      <input v-model="newCar" @keyup.enter="addCar">
      <button @click="addCar">+ Add</button>

      <li v-for="car in cars">
        {{ car }}
      </li>
      <car-list :cars="cars" />
    </div>
  </body>
</html>

<script>

Vue.component('car-list', {
 template:
   <ul>
    <li v-for="car in cars">{{ car }}</li>
   </ul>
 })

  const app = new Vue({
    el: '#root',
    component: [
      'car-list'
    ],
    data: {
      cars: [
      'audi',
      'bmw',
      'mercu'
    ],
    newCar: ''
  },
  methods: {
    addCar: function() {
      this.cars.push(this.newCar)
      this.newCar = ''
    }
  },
  computed: {
    carzName: function() {
      if (this.newCar.length > 1) {
        return this.newCar + 'y'
      }
    }
  }
  })
</script>

Thanks to anyone who is willing to explain me where the problem is at.

2
  • 2
    Multi-line strings must be enclosed in back-ticks in JavaScript. Add a back-tick before <ul> and another after </ul>. Commented Sep 10, 2020 at 13:40
  • 2
    globally applying Vue.component makes component: ['car-list'], redundent Commented Sep 10, 2020 at 13:41

1 Answer 1

2

Template needs to be string. Add backtick quotes around it.

Vue.component('car-list', {
  template: `
    <ul>
      <li v-for="car in cars">{{ car }}</li>
    </ul>`
})

Edit: To iterate more on @Lawrence Cherone comment there is no need to register the component globally with Vue.component if you are gonna register it within your vue app.

Version 1: Register globally

Vue.component('car-list', {
  template: `
    <ul>
      <li v-for="car in cars">{{ car }}</li>
    </ul>`
 })

and then remove component property from vue app

const app = new Vue({
  el: '#root',
  data: {}
})

Verison 2: Register within app

const carList = {
  template: `
    <ul>
      <li v-for="car in cars">{{ car }}</li>
    </ul>`
}

And then

const app = new Vue({
   el: '#root',
   component: {
      carList
   },
   data: {}
})

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

1 Comment

Yes, that did the trick. Didn't notice them in the video, they're tiny.

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.