Why two Vue.js identical components with different prop names give different results?

自作多情 提交于 2019-12-01 21:51:01

That's due to the casing used for props: https://vuejs.org/v2/guide/components-props.html#Prop-Casing-camelCase-vs-kebab-case

If you're using mydataTwo as the prop in the component declaration, then you will need to use v-bind:mydata-two in the template, not v-bind:mydataTwo.

Instead of doing this:

<child-two :mydataTwo="mydata"></child-two>

You should be doing this:

<child-two :mydata-two="mydata"></child-two>

See proof-of-concept example:

Vue.component('child-one',{
  template:'#child-one',
  props:['one'] 
});

Vue.component('child-two',{
  template:'#child-two',
  props:['mydataTwo'] 
});

let app = new Vue({
  el:'#app',
  data:{
    welcome:'Hello World',
    mydata:[]
  },
  methods:{
    getdataApi(){
      fetch( "https://jsonplaceholder.typicode.com/users").then(r => r.json()).then( (r) => {
        this.mydata = r;
      }); 
    } 
  },
  mounted:function(){ 
    this.getdataApi();
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">  
  <ul>
    <child-one :one="mydata"></child-one>
    
    <!-- Fix: use `mydata-two` instead of `mydataTwo` -->
    <child-two :mydata-two="mydata"></child-two>
    <!-- /Fix -->
  </ul>
</div>

<!-- child one template -->
<script type="text/x-template" id="child-one"> 
    <ul>
      LIST ONE
      <li v-for="item,i in one"> {{i}} {{item.name}} {{item.username.name}} {{item.email}} </li>   
    </ul>
</script> 

<!-- child two template -->
<script type="text/x-template" id="child-two"> 
    <ul>
      LIST TWO
      <li v-for="item,i in mydataTwo"> {{i}} {{item.name}} {{item.username.name}} {{item.email}} </li>   
    </ul> 
</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!