在vue3中通过道具传递的组件

时间:2020-09-24 22:53:45

标签: vue.js vuejs3

在Vue 2中,我有一个自定义的路由解决方案,在某些情况下涉及将组件作为道具进行交付。

<template>
    <div>
        <component :is="component" :data="componentData"/> 
    </div>
</template>

<script>
export default {
    props: {
        component: {}
        componentData: {}
    }
}
</script>

在vue 3中,这会发出警告:“ Vue收到了被设为反应性的组件”。

是否有避免此警告的方法?是否可以将组件动态添加到组件?

如果您有未在App.components中全局注册的组件数组,是否可以在没有警告的情况下呈现它们?

在Vue2中,这是一个较旧的问题: Is it possible to pass a component as props and use it in a child Component in Vue?

请注意,上面的代码在Vue2中工作正常,这个问题专门关于Vue3。

1 个答案:

答案 0 :(得分:1)

阻止错误的解决方案似乎是在设置数据之前将我的组件或组件列表包装在markRaw中。这会使vue3停止响应。

Mark Raw

<script>
import Page1     from './pages/page1.vue'
import Page2     from './pages/page2.vue'
import Page3     from './pages/page3.vue'
import Page4     from './pages/page4.vue'
import Page5     from './pages/page5.vue'
import {markRaw} from "vue"

const components = markRaw({
    Page1, Page2, Page3, Page4, Page5
})

export default {
    data() {
        return {
            components
        }
    }
}
</script>