javascript - 如何将 v-bind 的值更新为 prop?
问题描述
这是我将数据“时间”和“breedKey”传递给孩子的父页面
<template>
<MyChildComponent v-bind:breedKey="breedKey" v-bind:time="time"> </MyChildComponent>
</template>
<script>
data() {
return {
time:[]
breedKey:[]
}
},
<script>
这是我成功获取值的子页面,但是当父值更改时,值没有更新。
props: ["breedKey", "time"],
data() {
return {
thedate: this.time,
topic: this.breedKey
}
解决方案
data
仅使用 props 值初始化。
如果你想有反应,你可以直接引用道具。
Vue.config.devtools = false;
Vue.config.productionTip = false;
const MyChildComponent = Vue.component('mychildcomponent', {
template: '#mychildcomponent',
props: ["breedKey", "time"]
})
var app = new Vue({
el: '#app',
components: {
MyChildComponent
},
data() {
return {
time : [],
breedKey: []
}
},
methods: {
addTime() {
this.time.push(Math.floor(Math.random() * 100));
},
addKey() {
this.breedKey.push(Math.floor(Math.random() * 100));
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/vuex@3.1.2/dist/vuex.js"></script>
<div id="app">
<div>
<button @click="addTime">Add time</button>
<button @click="addKey">Add key</button>
<mychildcomponent v-bind:breed-key="breedKey" v-bind:time="time" />
</div>
</div>
<div>
<div style="display:none">
<div id="mychildcomponent">
<div>
time : {{ time }}
breedKey : {{ breedKey }}
</div>
</div>
</div>
推荐阅读
- r - 根据值 r 查找行号
- c++ - 这是维护大型 C++ 项目的良好目录结构吗?
- agent-based-modeling - Repast Java:如何在没有 GUI 的情况下直接在 Eclipse 中运行模型以及如何在没有 Eclipse 的情况下以无头模式运行它
- amazon-web-services - 将mysql数据库转换为dynamodb模型
- javascript - 参数结果隐式具有任何类型
- python - 如何组合长度不等的数据帧
- microservices - 微服务的事务管理
- python - 多次调用类中的方法,变量保持一致
- c# - 在桌面 C# 中检测文件选择右键单击
- c# - 以秒或分钟或小时或天计算期限到期日期