【发布时间】:2020-01-31 21:09:46
【问题描述】:
我的主要组件 - 主页
一个非常简单的组件,我将 fetch 变量传递给另一个组件。
<template>
<Page actionBarHidden="true">
<ComponentA :api="api.somevariable"></ComponentA>
</Page>
</template>
<script>
import axios from "axios";
import ComponentA from "./ComponentA.vue";
export default {
data() {
return {
isLoading: false,
result: []
};
},
components: {
ComponentA,
},
created() {
this.loadData();
},
methods: {
async loadData() {
let self = this;
console.log("fetch");
self.isLoading = true;
const { data } = await Endpoints.get();
self.isLoading = false;
self.api = data;
console.log(data); // returns the data as intended
}
}
</script>
组件A也很简单
<template>
<Label :text="somevariable"></Label>
</template>
<script>
export default {
data() {
return {
somevariable: 0
};
},
props: {
api: {
type: Number,
required: true
}
},
mounted() {
this.somevariable = this.api;
}
};
</script>
我得到的错误是 [Vue warn]: Invalid prop: type check failed for prop "api"。值为 NaN 的预期数字,在组件 A 中未定义,在对 console.logs 进行一些引用和重新引用后,它实际上获取了该值。我不确定为什么会这样,我的方法是错误的吗?这让我很沮丧,已经有几个小时想不通了。
【问题讨论】:
-
据我所知,在父组件中没有 api.somevariable 直到异步请求完成
-
我觉得它试图在实际获取变量之前获取它。也许,如果是这样,如何解决这个问题
-
也许让创建的异步,或者在你有数据之前不渲染组件A
-
还是不行
标签: vue.js nativescript nativescript-vue