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

Attempting to pass props through render in Vue.js and watch them

我在使用 vue.js 中的 CreateElement/render 将道具从 parent 向下传递到创建的 child 然后观看它时遇到了问题。

这是我的 parent 组件

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
      }
    })
  }
});

所以你可以在这里看到,我获取了某些总线的发射值,然后将其传递给 useColor。然后我想将此值作为 useThisColor 传递给我的渲染函数。

那就是child.

<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

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

我想这里的问题是您没有在 MyDrawing 组件上定义 属性、useThisColor

这是一个example.