尝试在Vue.js中通过渲染传递道具并观察它们

时间:2017-05-20 00:55:39

标签: javascript vue.js parent-child state

我在使用vue.js中的CreateElement / render然后观看它时,通过已创建的子项向父母传递道具时遇到问题。

这是我的父组件

Vue.component('my-drawing', MyDrawing)

new Vue({
  el: '#drawing',
  mounted() {
    Bus.$on('emitColorSelection', (emitString) => {
      console.log("inside socket.js/my-drawing and emitString is ", emitString);
      this.useColor = emitString;
      console.log('inside socket.js/my-drawing and this.useColor after set is ', this.useColor);
    })

  },
  data() {
    return {
      channel2: null,
      canvases: [],
      useColor: 'rgba(255, 0, 0, 1)'
    }
  },
  render(createElement) {
    return createElement(MyDrawing, {
      props: {
        useThisColor: this.useColor
      }
    })
  }
});

所以你可以在这里看到我取一些总线的emit的值然后我把它传递给useColor。我想将此值作为useThisColor传递给我的渲染函数。

然后是孩子。

<template>
  //my template stuff
</template>

<script>
//stuff
  watch: {
    useThisColor (n, o) {
      console.log("useThisColor watch, ", n, o) // n is the new value, o is the old value.
    }
  } 
//stuff continues

所以这个表标签没有输出。我还尝试将道具放在模板中无效,并试图将其输出到Updated:标签上。我还尝试使用引号在父级中设置道具。到目前为止没有任何工作,我有点困惑。如果有人有任何想法,请告诉我。

1 个答案:

答案 0 :(得分:6)

我希望这里的问题是你没有在MyDrawing组件上定义属性useThisColor

这是example