我在vue中使用CreateElement/render将父对象的props 向下传递到已创建的子对象时遇到问题.然后看.

这是我的父组件

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

所以这个手表标签不会输出.我也try 过将props 放在模板中而不产生任何效果,并try 将其输出到Updated:标签上.我还try 使用引号在父对象中设置props .到目前为止,一切都不管用,我有点困惑.如果有人有任何 idea ,请告诉我.

推荐答案

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

这是example美元.

Vue.js相关问答推荐

为什么将 name 与 router-link 一起使用比仅使用 to 与 path 更好?

Vue index.html favicon 问题

有条件地在 Vue 中添加一个 CSS 类

可以同时使用 Tailwind css 和 Bootstrap 4 吗?

如何判断 Vue 组件是否处于活动状态

使用 VueJS 在 v-for 中转换

Vuetify 单选按钮未显示为选中状态

命名空间模块身份验证的重复命名空间身份验证

带有子菜单的 Vuetify 导航抽屉

Laravel Blade 模板将数据传递给 Vue JS 组件

Vue.js:从 parent父Vue 获取数据

Vuex - 何时从 http 服务器加载/初始化存储数据

Vue 动态组件事件绑定

如何确定 Vue 何时完成更新 DOM?

'不提供名为'createRouter'的导出'' vue 3、vite 和 vue-router

是否可以从 vue-devtools 中排除 vue 事件和 vuex Mutations?

使用 axios 和 vue.js 取消先前的请求

脚本npm run dev和npm run watch是做什么用的?

Vuejs 不会在 HTML 表格元素中呈现组件

在另一个 props 的验证器中访问 props 值