是否可以将组件作为道具传递并在Vue中的子组件中使用它?

Vic*_*ira 36 javascript vue.js vue-component vuejs2

在Vue 2.0应用程序中,假设我们有组件A,B和C.

声明,注册和使用B.

是否有可能将C从A传递给B?

像这样的东西:

<template>
  <div class="A">
    <B :child_component="C" />
  </div>
</template>
Run Code Online (Sandbox Code Playgroud)

并以某种方式在B中使用C.

<template>
  <div class="B">
    <C>Something else</C>
  </div>
</template>
Run Code Online (Sandbox Code Playgroud)

动机:我想创建一个通用组件B,用于A但从A其子级接收C.实际上AB多次使用不同的'C'来表示它.

如果这种方法不正确,在Vue中这样做的正确方法是什么?

回答@Saurabh

而不是作为道具传递,我尝试了B.内部的建议.

<!-- this is where I Call the dynamic component in B -->

<component :is="child_component"></component>

//this is what I did in B js
components: {
 equip: Equipment
}, 
data () {
 return {
   child_component: 'equip',
   _list: []
 }
}
Run Code Online (Sandbox Code Playgroud)

基本上我试图渲染设备,但动态的方式

我在控制台和空白页面中收到3个错误

[Vue警告]:在/home/victor/projetos/tokaai/public/src/components/EquipmentFormItem.vue中渲染组件时出错:

未捕获的TypeError:无法读取未定义的属性"name"

TypeError:无法读取undefined的属性'setAttribute'

显然我做错了什么

Jon*_*ker 17

加起来:

<!-- Component A -->
<template>
  <div class="A">
    <B>
      <component :is="child_component"></component>
    </B>
  </div>
</template>

<script>
import B from './B.vue';
import Equipment from './Equipment.vue';

export default {
  name: 'A',
  components: { B, Equipment },
  data() {
    return { child_component: 'equipment' };
  }
};
</script>

<!-- Component B -->
<template>
  <div class="B">
    <h1>Some content</h1>
    <slot></slot> <!-- Component C will appear here -->
  </div>
</template>
Run Code Online (Sandbox Code Playgroud)

  • 我们必须导入所有带有 ` &lt;component :is="child_component"&gt;&lt;/component&gt;` 标签的子组件。如果我们只需要来自父级的特定子组件,它会不会让它变慢。有什么解决方法可以让我们在父组件中导入组件并将其传递给子组件吗? (2认同)

Sau*_*abh 16

你可以使用特殊属性is来做这种事情.动态组件的示例及其用法可在此处找到.

您可以使用相同的挂载点,并使用保留元素在多个组件之间动态切换,并动态绑定到其is属性:

您的代码如下所示:

<template>
  <div class="B">
    <component :is="child_component"> Something else</component>
  </div>
</template>
Run Code Online (Sandbox Code Playgroud)


小智 7

这是通过另一个组件的 props 转发自定义组件的解决方案

:is是特殊属性,它将用于替换您的实际组件,如果您尝试将其用作组件中的道具,它将被忽略。幸运的是,您可以使用其他类似的东西el,然后将其转发为component如下所示:

<template>
  <div>
    <component :is="el">
      <slot />
    </component>
  </div>
</template>
<script>
  export default {
    name: 'RenderDynamicChild',
    props: {
        el: {
            type: [String, Object],
            default: 'div',
        },
    },
  }
</script>
Run Code Online (Sandbox Code Playgroud)

您在属性中使用的任何有效元素都el将用作子组件。它可以是 html 或对自定义组件的引用,也div可以是组件声明中指定的默认值。

将自定义组件传递给 prop 有点棘手。人们会假设您在父组件的属性中声明components,然后将其用作el属性,但这不起作用。相反,您需要将动态组件放入datacomputed属性中,以便可以在模板中将其用作道具。另请注意,AnotherComponent不需要在components属性中声明。

<template>
  <RenderDynamicChild :el="DynamicComponent">
    Hello Vue!
  </RenderDynamicChild>
</template>

<script>
import RenderDynamicChild from './DynamicChild';
import AnotherComponent from './AnotherComponent';

export default {
  name: "ParentComponent",
  components: { DynamicChild },
  data() {
    return {
      DynamicComponent: AnotherComponent,
    };
  },
};
</script>
Run Code Online (Sandbox Code Playgroud)

使用动态组件的计算属性可以让您轻松地在组件之间切换:

<script>
import DynamicChild from './DynamicChild';
import AnotherComponent from './AnotherComponent';

export default {
  name: "ParentComponent",
  components: { DynamicChild },
  data() { return { count: 0 } },
  computed: {
    DynamicComponent() {
      return this.count % 2 > 1 ? AnotherComponent : 'article';
    },
  },
};
</script>
Run Code Online (Sandbox Code Playgroud)

增加this.count交替AnotherComponent和简单的articlehtml 元素。