Vue未定义错误,不确定我的代码出了什么问题

5jdjgkvh  于 2023-05-01  发布在  Vue.js
关注(0)|答案(1)|浏览(171)

我得到了“vue is not defined error”,我已经尝试了很多我在互联网上看到的东西。我还是一个初学者,正在努力学习我的第一个学校作业。我还评论了一些我已经尝试过或认为我不需要的部分。谁能帮帮我,告诉我我做错了什么?我有一个HTML页面,我复制并粘贴到'窗体'组件,现在我必须将其链接到'应用程序。vue' src.
原始html

<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

<div id="main">
<form v-cloak>
    <h1>Facilities</h1>

    <ul>
        <!-- Loop through the facilities array, assign a click handler, and set or
             remove the "active" css class if needed -->
        <li v-for="facililty in facilities" v-on:click="toggleActive(facililty)" v-bind:class="{ 'active': facililty.active}">
            <!-- Display the name and price for every entry in the array .
                Use the formatCurrency method for formatting the price -->
            {{facililty.name}} <span>{{facililty.price | currency}}</span>
        </li>
    </ul>

    <div class="total">
        <!-- Calculate the total price of all chosen facililtys. Format it as currency using formatCurrency. -->
        Total: <span>{{total() | currency}}</span>
    </div>

</form>
</div>
<script>

  Vue.createApp({
    data() {
        // The model properties. The view should loop
        // through the facilities array and generate a li
        // element for every one of its items.
        return { facilities: [
            {
                name: 'Ballroom',
                price: 5000,
                active:true
            },{
                name: 'Backyard',
                price: 400,
                active:false
            },{
                name: 'Wellness Area',
                price: 250,
                active:false
            },{
                name: 'Restaurant',
                price: 220,
                active:false
            }
        ]
        }
    },
    methods: {
        // this method will toggle an item to make it active/inactive (no change necessary)
        toggleActive: function(s){
            s.active = !s.active;
        },
        // method to calculate the total amount
        total: function(){
            // variable to hold total
            var total = 0;

            this.facilities.forEach(function(s){
                if (s.active){
                    total+= s.price;
                }
            });

            //loop through facilities
            //
                //check whether item is active
                //
                    //sum up for total
                    //
                //
            //

           return total;
        },
        // method to format as currency
        formatCurrency(value) {
            
            // put dollar sign in front of value and show 2 deci,al places e.g $500.00
            return value;
  }
    }
  }).mount('#main')
</script>

<style>
@import url(https://fonts.googleapis.com/css2?family=Montserrat:wght@700&family=Shadows+Into+Light);

/* v-cloak Hide un-compiled mustache bindings
until the Vue instance is ready */

[v-cloak] {
  display: none;
}

*{
    margin:0;
    padding:0;
}

body{
    font:15px/1.3 'Open Sans', sans-serif;
    color: #5e5b64;
    text-align:center;
}

a, a:visited {
    outline:none;
    color:#389dc1;
}

a:hover{
    text-decoration:none;
}

section, footer, header, aside, nav{
    display: block;
}

/*-------------------------
    The order form
--------------------------*/

form{
    background-color: #8931EF;
    border-radius: 2px;
    box-shadow: 0 1px 1px #ccc;
    width: 400px;
    padding: 35px 60px;
    margin: 50px auto;
}

form h1{
    color:#fff;
    font-size:64px;
    font-family:'Cookie', cursive;
    font-weight: normal;
    line-height:1;
    text-shadow:0 3px 0 rgba(0,0,0,0.1);
}

form ul{
    list-style:none;
    color:#fff;
    font-size:20px;
    font-weight:bold;
    text-align: left;
    margin:20px 0 15px;
}

form ul li{
    padding:20px 30px;
    background-color:#a5949a;
    margin-bottom:8px;
    box-shadow:0 1px 1px rgba(0,0,0,0.1);
    cursor:pointer;
}

form ul li span{
    float:right;
}

form ul li.active{
    background-color:#87E911;
}

div.total{
    border-top:1px solid rgba(255,255,255,0.5);
    padding:15px 30px;
    font-size:20px;
    font-weight:bold;
    text-align: left;
    color:#fff;
}

div.total span{
    float:right;
}

</style>

form.vue(只是从HTML页面复制粘贴)

<!-- <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> -->
<template>
<div id="main">
<form v-cloak>
    <h1>Facilities</h1>

    <ul>
        <!-- Loop through the facilities array, assign a click handler, and set or
             remove the "active" css class if needed -->
        <li v-for="facililty in facilities" v-on:click="toggleActive(facililty)" v-bind:class="{ 'active': facililty.active}">
            <!-- Display the name and price for every entry in the array .
                Use the formatCurrency method for formatting the price -->
            {{facililty.name}} <span>{{facililty.price | currency}}</span>
        </li>
    </ul>

    <div class="total">
        <!-- Calculate the total price of all chosen facililtys. Format it as currency using formatCurrency. -->
        Total: <span>{{total() | currency}}</span>
    </div>

</form>
</div>
</template>
<script>
// import Form from './Form.vue'
export default {
    data() {
        return {
            name: '',
            price: ''
        }
    }
};
Vue.createApp({
    data() {
                // The model properties. The view should loop
        // through the facilities array and generate a li
        // element for every one of its items.
        return { facilities: [
            {
                name: 'Ballroom',
                price: 5000,
                active:true
            },{
                name: 'Backyard',
                price: 400,
                active:false
            },{
                name: 'Wellness Area',
                price: 250,
                active:false
            },{
                name: 'Restaurant',
                price: 220,
                active:false
            }
        ]
    }
},
methods: {
    // this method will toggle an item to make it active/inactive (no change necessary)
    toggleActive: function(s){
        s.active = !s.active;
    },
    // method to calculate the total amount
    total: function(){
        // variable to hold total
        var total = 0;
        
        this.facilities.forEach(function(s){
            if (s.active){
                total+= s.price;
            }
        });
        
            //loop through facilities
            //
                //check whether item is active
                //
                    //sum up for total
                    //
                //
            //

        return total;
    },
    // method to format as currency
    formatCurrency(value) {
        // put dollar sign in front of value and show 2 deci,al places e.g $500.00
        return value;
    }
}
}).mount('#main')
</script>

<style>
@import url(https://fonts.googleapis.com/css2?family=Montserrat:wght@700&family=Shadows+Into+Light);

/* v-cloak Hide un-compiled mustache bindings
until the Vue instance is ready */

[v-cloak] {
  display: none;
}

*{
    margin:0;
    padding:0;
}

body{
    font:15px/1.3 'Open Sans', sans-serif;
    color: #5e5b64;
    text-align:center;
}

a, a:visited {
    outline:none;
    color:#389dc1;
}

a:hover{
    text-decoration:none;
}

section, footer, header, aside, nav{
    display: block;
}

/*-------------------------
    The order form
--------------------------*/

form{
    background-color: #8931EF;
    border-radius: 2px;
    box-shadow: 0 1px 1px #ccc;
    width: 400px;
    padding: 35px 60px;
    margin: 50px auto;
}

form h1{
    color:#fff;
    font-size:64px;
    font-family:'Cookie', cursive;
    font-weight: normal;
    line-height:1;
    text-shadow:0 3px 0 rgba(0,0,0,0.1);
}

form ul{
    list-style:none;
    color:#fff;
    font-size:20px;
    font-weight:bold;
    text-align: left;
    margin:20px 0 15px;
}

form ul li{
    padding:20px 30px;
    background-color:#a5949a;
    margin-bottom:8px;
    box-shadow:0 1px 1px rgba(0,0,0,0.1);
    cursor:pointer;
}

form ul li span{
    float:right;
}

form ul li.active{
    background-color:#87E911;
}

div.total{
    border-top:1px solid rgba(255,255,255,0.5);
    padding:15px 30px;
    font-size:20px;
    font-weight:bold;
    text-align: left;
    color:#fff;
}

div.total span{
    float:right;
}

</style>

App.vue

<script setup>
// import HelloWorld from './components/HelloWorld.vue'
// import TheWelcome from './components/TheWelcome.vue'
import Form from './components/Form.vue'
</script>

<template>
  <header>
    <!-- <img alt="Vue logo" class="logo" src="./assets/logo.svg" width="125" height="125" />

    <div class="wrapper">
      <HelloWorld msg="You did it!" />
    </div> -->
  </header>

  <main>
    <!-- <TheWelcome /> -->
    <!-- <Form /> -->
  </main>
</template>

<script>
new Vue({
  el: '#app',
  components: {
    Form
  }
});
// export default {
//   // name: 'App',
//   components: {
//     Form
// }
// };

</script>

<style>
@import './assets/base.css';

#app {
  max-width: 1280px;
  margin: 0 auto;
  padding: 2rem;

  font-weight: normal;
}

header {
  line-height: 1.5;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
}

a,
.green {
  text-decoration: none;
  color: hsla(160, 100%, 37%, 1);
  transition: 0.4s;
}

@media (hover: hover) {
  a:hover {
    background-color: hsla(160, 100%, 37%, 0.2);
  }
}

@media (min-width: 1024px) {
  body {
    display: flex;
    place-items: center;
  }

  #app {
    display: grid;
    grid-template-columns: 1fr 1fr;
    padding: 0 2rem;
  }

  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }

  .logo {
    margin: 0 2rem 0 0;
  }
}
</style>
8ehkhllq

8ehkhllq1#

当使用一个完整的脚手架项目时,createApp()函数会在main.js文件中调用。该文件称为入口点。下面是App.vueForm.vuemain.js(如果使用TypeScript,则为.ts)的总体布局。

// main.js
import { createApp } from 'vue'
import App from './App.vue'

import './assets/main.css'

// this may need to be #main depending on what's in your index.html file
createApp(App).mount('#app')
// App.vue
<script setup>
import Form from './components/Form.vue'
</script>

<template>
  <header>
  </header>
  <main>
    <!-- <TheWelcome /> -->
    <Form />
  </main>
</template>
<style>
</style>
// Form.vue
<script>
export default {
    data() {
        return {
            name: '',
            price: '',
            facilities: [
                {
                    name: 'Ballroom',
                    price: 5000,
                    active: true
                }, {
                    name: 'Backyard',
                    price: 400,
                    active: false
                }, {
                    name: 'Wellness Area',
                    price: 250,
                    active: false
                }, {
                    name: 'Restaurant',
                    price: 220,
                    active: false
                }
            ]
        }
    },
    methods: {
        toggleActive: function (s) {
            s.active = !s.active;
        },
        // method to calculate the total amount
        total: function () {
            // ...
        },
        // method to format as currency
        formatCurrency(value) {
            // ...
        }
    }
}
</script>
<style></style>

相关问题