Click here to Skip to main content
15,883,928 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have followed this site: VueJS - Instances[^] to create my own instance.

However, I got this error:
Errors compiling template:

Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side-effects in your templates, such as <script>, as they will not be parsed.

3  |      <head>
4  |          <title>VueJs Instance</title>
5  |          <script type="text/javascript" src="js/vue.js"></script>
   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6  |      </head>
   |  ^^^^^^^^^^^
7  |      <body>
   |  ^^^^^^^^^^
8  |          <div id="vue_det">
   |  ^^^^^^^^^^^^^^^^^^^^^^^^^^
9  |              <h1>First name: {{firstname}}</h1>
   |  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10 |              <h1>Last name: {{lastname}}</h1>
   |  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11 |              <h1>{{mydetails}}</h1>
   |  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12 |          </div>
   |  ^^^^^^^^^^^^^^
13 |          <script type="text/javascript" src="../instances/vue_instance.js"></script>
   |  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14 |      </body>
   |  ^^^^^^^^^^^
15 |  </div>
   |  ^^^^^^


What have I done wrongly? How can I fix it?

What I have tried:

my template in the .vue component file:
<template>
    <div class="home">
        <head>
            <title>VueJs Instance</title>
            <script type="text/javascript" src="js/vue.js"></script>
        </head>
        <body>
            <div id="vue_det">
                <h1>First name: {{firstname}}</h1>
                <h1>Last name: {{lastname}}</h1>
                <h1>{{mydetails}}</h1>
            </div>
            <script type="text/javascript" src="../instances/vue_instance.js"></script>
        </body>
    </div>


And this is my instance script (vue_instance.js):
var vm = new Vue({
    el='#vue_det',
    data: {
        firstname: "James",
        lastname: "Bond",
        address: "007"
    },
    methods: {
        mydetails: function () {
            return this.lastname+ ", " + this.firstname + " " + this.lastname;
        }
    }
})


I am using VIsual Studio 2022.
Posted
Updated 7-Dec-21 22:44pm
v2

1 solution

Quote:
Avoid placing tags with side-effects in your templates, such as <script>, as they will not be parsed.
The error message is perfectly clear - you cannot place a <script> tag in your <template>.

You have two <script> tags in your template. They need to be in the containing page instead, as the tutorial you've linked to clearly shows.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900