Problems with local components of vue


< html lang= "en" >
< head >

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

< / head >
< body >

<!-- TODO:  -->
<div id="app">
    <Apptext
        v-for="item in list"
        :eatList="item"
        v-bind:key="item.id"
    ></Apptext>
</div>
<script>
  
    var app = new Vue({
        el: "-sharpapp", 
        components:{
            Apptext:appText
        },
        data: {
            list:[
                { id: 0, text: "" },
                { id: 1, text: "" },
                { id: 2, text: "" }
            ]
        },
    })
    var appText = {
        props:["eatList"],
        template: "<li>{{ eatList.text }}</li>"
    }
</script>

< / body >
< / html >

A recent example of learning vue, to read official documents is rewritten by itself with local components, but browsers always report errors, but there is no problem with global components

Mar.22,2021

appText is locally registered right, but Apptext this is what, does not specify, oh


you write this is obviously a component will report an error ah, appText this should be apptext


component registration steps are right, wrong in the appText variable, when you use this variable, its value is undefined, because you did not define it before use.
add that when passing values for subcomponents, use the naming pattern of kebab-case (dash) instead of PascalCase (hump naming)


register hump quote lowercase dash


Emmmm, this is caused by the variable promotion of var , because you put the assignment of the appText object after the Vue instantiation .

is actually equivalent to this:

var appText = undefined

var app = new Vue({
  el: '-sharpapp',
  components: {
    Apptext: appText // appTextundefined
  },
  data: {
    list:[
      { id: 0, text: '' },
      { id: 1, text: '' },
      { id: 2, text: '' }
    ]
  },
})

appText = {
  props:['eatList'],
  template: '<li>{{ eatList.text }}</li>'
}

change the assignment of appText before new Vue .

PS: : eatList= "item" needs to be written as : eat-list= "item"

Menu