【发布时间】:2020-07-25 06:13:06
【问题描述】:
我已经从我的根组件收到了道具。通过我测试的<template>,当根组件中的值发生更改时,{{firstdata}} 会更新,但{{topic}} 仍然与它获得的第一个值相同。好像this.firstdata 只存储一次数据而无需进一步更新。
我使用return {topic: this.firstdata 是因为我需要在我的java 脚本中使用topic,因为我不能在javascript 部分直接调用{{firstdata}}。有什么解决方案可以为我提供反应性更新topic?
<template>
{{firstdata}}
{{ this.topic }}
</template>
<script>
export default {
props: ["firstdata", "seconddata"],
data() {
return {
topic: this.firstdata
};
},
</script>
这就是我从父级获取更新值的方式(我提到的第一个数据是
breedKey)
<b-button v-on:click="currentBreed = 0" > {{ breeds[0].name }} </b-button>
<b-button v-on:click="currentBreed = 1" > {{ breeds[1].name }} </b-button>
<ChildCompo v-bind:breedKey="breedKey" v-bind:time="time"> </ChildCompo>
<script>
data() {
const vm = this;
return {
currentBreed: 0,
time:[],
breeds: [
{ name: "" , key: "" }, // Initially empty values
{ name: "" , key: "" }, // Initially empty values
{ name: "" , key: "" }, // Initially empty values
]
}
},
async created() {
try {
this.promise = axios.get(
"https://www.mustavi.com/Trends/"
);
const res = await this.promise;
this.topic0 = res.data.data[0].Trends;
this.topic1 = res.data.data[1].Trends;
this.topic2 = res.data.data[2].Trends;
this.breeds[0].name = this.breeds[0].key = this.topic0;
this.breeds[1].name = this.breeds[1].key = this.topic1;
this.breeds[2].name = this.breeds[2].key = this.topic2;
this.time = res.data.data[0].DT;
} catch (e) {
console.error(e);
}
},
computed: {
breedKey() {
return this.breeds[this.currentBreed].key;
}
},
</script>
【问题讨论】:
-
不明白你所说的“因为我不能在javascript部分直接调用
{{ firstdata }}。”? -
为什么不能在组件中使用
firstdata?您在设置topic时确实使用了它,并且您可以在任何需要的地方继续以相同的方式使用它。 -
您能否在此处查看我的代码jsfiddle.net/L205bt84 在我的 PC 中,子组件中第 35 行的
search_params.set('param1', this.topic);可以工作,但它仍然是它获得的第一个值,不知道为什么在浏览器中它甚至无法获得第一个值,如果您尝试将this.topic更改为'China'您将看到 API 工作正常
标签: javascript vue.js vuejs2 vue-component vue-props