【问题标题】:Vue JS | Component transferingVue JS |组件转移
【发布时间】:2020-06-26 04:28:42
【问题描述】:

想将组件作为变量传递。

例如: 我们有 3 个组件:A、B、C。

A.vue

<template>
<B>:redirect_object=C</B>
</template>
<script>
import B from "B";
import C from "C";
export default {
    name: "A",
    components: {B,C}
}
B.vue
...
<script>
export default {
    name: "B",
    props: {
        title: String,
        content: String,
        icon: String,
        redirect_object: Object // This var should contain coming component

    },
    methods: {
        do_redirect: function () {
            if (this.redirect_object)
                this.$store.commit('change_module', this.redirect_object)
        }
    }
}

C.vue

...
<script>
export default {
    name: "C"
}

所以。我想通过 A.vue 将 C.vue 组件转移到 B.vue 到 props 中的 redirect_object 变量中。

尝试在A中导入C组件,但我无法将其转移到B中。

【问题讨论】:

    标签: javascript vue.js vuejs2


    【解决方案1】:

    为什么要这样做?我认为像这样传输组件不是一个好习惯。组件的一般做法是import组件中的组件(父-子)并动态加载具有:is属性的组件。

    Docs with examples for dynamic components

    【讨论】:

    • 看起来像кастыли,真的
    • 您对代码的主要想法是什么?您不应该有&lt;B&gt;:redirect_object=C&lt;/B&gt;,其中C 是组件引用。您应该在组件&lt;B&gt; 中导入&lt;C&gt; 并在其中注册。
    • 别担心,没关系。我们每天都在学习新事物。最好的问候,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 2019-12-10
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多