javascript - 如何通过 props 传递导入的组件?

标签 javascript vue.js vue-component

有没有办法可以通过 props 将组件从父组件传递给子组件,而不必从子组件导入它?

父组件:

<template>
  <ChildComponent :component="componentName">
</template>
<script>
  import componentName from '@/components/componentName.vue'
</script>

子组件:

<template>
   <div>
      <component :is="component">
   </div>
</template>
<script>
  props: {
    component: Object
  }
</script>

目前,在此设置中,我收到未知的自定义事件您是否正确注册了组件?错误。我知道它希望我在子组件中注册该组件,但我不想从子组件中导入该组件。有没有办法通过 props 将导入传递给 child ?这不可能吗?

最佳答案

你真的很接近,问题出在你的第一个模板中:

<template>
  <ChildComponent :component="componentName">
</template>
<script>
  // this is not available in template scope
  import componentName from '@/components/componentName.vue'

  export default {
    data: {
      // this, however, is available!
      componentName
    }
  }
</script>

关于javascript - 如何通过 props 传递导入的组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57611340/

相关文章:

vue.js - Vuejs 范围错误 : Invalid string length

javascript - 什么是窗口 ["foo"]=function() {alert ("foo");}

css - 在vue js中更改单选按钮的样式

Vue.js - 使用 Vuelidate url 域应与电子邮件域匹配

javascript - 嵌套表单 - vue

typescript - Vue3 Typescript Prop 要么显示错误,要么根本不显示任何内容

javascript - 页面加载时显示 div,完成后隐藏

javascript - 字段变化触发功能

javascript - 在事件 IONIC V1 后增加 Firebase 子值

javascript - 使用 jquery 动态调用现有的 div