gpt4 book ai didi

javascript - 将数据从父组件传递到子组件

转载 作者:行者123 更新时间:2023-12-02 14:28:30 26 4
gpt4 key购买 nike

我有一个父组件,如下

<template>
<div class="row">
<mappings mapping-list="{{currentMappings}}"></mappings>
</div>
</template>

<script>
import Mappings from './content/Mappings.vue';

export default {
data () {
return {
currentMappings: Array
}
},
created () {
this.readData();
},
methods: {
readData () {
this.$http.get('data/Books.xml').then((response)=> {
var x2js = new X2JS();
var jsonObj = x2js.xml_str2json( response.body );
this.getMappings(jsonObj.WAStatus);
});
},
getMappings (jsonObj) {
this.currentMappings = jsonObj.WSSMappings;
}
},
components: { Mappings }
};
</script>

和一个子“映射”组件如下

<template>
<div class="col-lg-4">
<div class="hpanel stats">
<div class="panel-body h-200">
<div class="stats-title pull-left">
<h3>Mappings</h3>
</div>
<div class="stats-icon pull-right">
<img src="images/mappings.png" class="browserLogo">

</div>
<div class="m-t-xl">
<table class="table table-striped table-hover">
<tr v-for="mapping in mappingList ">
<td>name - {{$index}}</td>
</tr>
</table>
</div>
</div>
<div class="panel-footer">
Current WSS - SA Mappings
</div>
</div>
</div>
</template>
<script>
export default {
props: {
mappingList:Array
},
created () {
console.log(this.mappingList);
// console.log(this.$parent.mappings);
}
}
</script>

我无法将数据从父组件传递到子组件。我正在尝试在这里使用 Prop 。我收到的错误是

main.js:2756[Vue warn]: Invalid prop: type check failed for prop "mappingList". Expected Array, got String. (found in component: <mappings>)

更新根据提出的建议,我已更新父组件如下

<template>
<div class="row">
<browser-stats></browser-stats>
</div>
<div class="row">
<mappings :mapping-list="currentMappings"></mappings>
</div>
</template>

<script>

import Mappings from './content/Mappings.vue';

export default {
data () {
return {
currentMappings: []
}
},
created () {
this.readData();
},
methods: {
readData () {
this.$http.get('data/WA_Status.xml').then((response)=> {
var x2js = new X2JS();
var jsonObj = x2js.xml_str2json( response.body );
this.getMappings(jsonObj.WAStatus);
});
},
getMappings (jsonObj) {
this.currentMappings = jsonObj.WSSMappings;
console.log(this.currentMappings.length);
console.log(JSON.stringify(this.currentMappings));
}
},
components: { Mappings }
};
</script>

和子“映射”组件如下

<template>
<div class="col-lg-4">
<div class="hpanel stats">
<div class="panel-body h-200">
<div class="stats-title pull-left">
<h3>Mappings</h3>
</div>
<div class="stats-icon pull-right">
<img src="images/mappings.png" class="browserLogo">

</div>
<div class="m-t-xl">
<table class="table table-striped table-hover">
<tr v-for="mapping in mappingList ">
<td>{{mapping._name}}</td>
</tr>
</table>
</div>
</div>
<div class="panel-footer">
Current WSS - SA Mappings
</div>
</div>
</div>
</template>
<script>
export default {
props: {
mappingList:[]
}
}
</script>

但是我得到一个空的映射列表,这意味着每当我执行 console.log(this.currentMappings.length); 时,我都会得到未定义的..你能让我知道我在这里做错了什么吗?

谢谢

最佳答案

1) 拳头问题

  data () {
return {
currentMappings: Array
}

应该是

  data () {
return {
currentMappings: []
}

2) 第二期

<mappings mapping-list="{{currentMappings}}"></mappings>

应该是:

<mappings :mapping-list="currentMappings"></mappings>

3) 第三期

    created () {
console.log(this.mappingList);
// console.log(this.$parent.mappings);
}

这每次都会返回空数组。因为 mappingList 通过 AJAX 更改,并且是在调用 created() 后发生的。尝试使用vue-devtool (chrome plugin)以获得更好的调试体验。

更新:为了能够观看异步更改(例如使用 AJAX 进行的更改),请考虑使用 watch 这种方式:

{
data(){
//..
},
watch:{
'currentMappings'(val){
console.log('currentMappings updated', currentMappings);
}
}
}

文档是 here .

关于javascript - 将数据从父组件传递到子组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38035777/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com