vue.js - 渲染后Vue将更新的道具发送给孩子
问题描述
VUE渲染后如何更新PROPS?
家长
const myProps = [];
window.onmessage = function(data) {
myProps.push(data)
}
new Vue({
el: el || '',
render: h => h(myComponent, {props:{myProps: myProps}}),
});
孩子
<template>...</template>
export default {
props: ['myProps'],
....
}
由于窗口消息在 myComponent 渲染后稍后出现..在这种情况下没有任何更新,只是得到空数组..在这种情况下收听更新道具的最佳方法是什么?
解决方案
myProps
在 vue 实例中定义为 data 属性,然后window.onmessage
在挂载的钩子中运行:
new Vue({
el: el || '',
data(){
return{
myProps : []
}
},
mounted(){
window.onmessage = (data)=> {
this.myProps.push(data)
}
},
render: function(h){return h(myComponent, {props:{myProps: this.myProps}}}),
});
推荐阅读
- sql - 如何从包含逗号分隔值的行中删除重复项?
- java - Java算术:为什么它们不是“9 = 9”?
- java - SpringBoot中如何连接MySql数据库?
- javascript - 为什么递归函数在返回数组上返回未定义?
- c - 左圆周运动
- python - pynput 在 MacOS 上返回太多字符
- xilinx - Xilinx Vivado:模块设计,每个模块端点的地址范围
- react-native - 如何在本机反应中处理繁重的 API 响应?
- eclipse - 如何从“查找/替换”或“搜索”更改 Eclipse 输入框区域的颜色
- node.js - 使用 find $in 数组增加文档