vue.js - Vue 在渲染后将更新后的 props 发送给子进程

标签 vue.js vue-component

VUE渲染后如何更新PROPS?

家长

const myProps = [];
window.onmessage = function(data) {
  myProps.push(data)
}

new Vue({
  el: el || '',
  render: h => h(myComponent, {props:{myProps: myProps}}),
});

child

<template>...</template>

export default {
  props: ['myProps'],
  ....
}

由于窗口消息在 myComponent 渲染后稍后出现..在这种情况下没有任何更新,只是得到空数组.. 在这种情况下,监听更新属性的最佳方式是什么?

最佳答案

myProps定义为vue实例中的数据属性,然后在挂载的钩子(Hook)中运行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}}}),
});

关于vue.js - Vue 在渲染后将更新后的 props 发送给子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64958427/

相关文章:

javascript - 如何使用方法更改 props 值 - Vue.js

javascript - 如何自定义vuetify轮播箭头?

vue.js - Keep-alive 在单个路由上而不是路由器 View 中的所有路由

javascript - 尝试运行 Mocha 测试时 require.context 不是函数

javascript - vue与php交互

javascript - Vue组件渲染时如何触发事件?

vue-component - 如何在 Vue 中公开包装的 &lt;input&gt;?

javascript - 如何在 Vue 中动态标记或标记 DOM 元素

javascript - 在另一个 Vue 插件中使用一个 Vue 插件

javascript - 如何发出改变 css 样式的事件