如何让VUE的组件名动态化

目前遇到一整个这样的需求,假设 product.vue 有如下内容:

<template lang="html">
  <div class="product-box">
    <!--头部组件-->
    <m-header></m-header>
    <!--/头部组件-->

    <!--中间区域-->
    <div class="product-center">
      <!--左侧组件-->
      <product-left></product-left>
      <!--/左侧组件-->

      <!--内容区域-->
      <div class="product-content">
        <product-module1 v-if="moduleName == 'module1'"></product-module1>
        <product-module2 v-else-if="moduleName == 'module2'"></product-module2>
        <product-module3 v-else-if="moduleName == 'module3'"></product-module3>
        <product-module4 v-else-if="moduleName == 'module4'"></product-module4>
      </div>
      <!--/内容区域-->
      
    </div>
    <!--/中间区域-->

    <!--底部组件-->
    <m-footer></m-footer>
    <!--/底部组件-->
  </div>
</template>

moduleName 控制content区域内的模块显示(只有一个模块显示),但是里面的模块的个数是不可预计的用if else-if的方式来判断太过于低效,每新增一个模块都会去手动判断一个模块的名称,有没有一个简便的方法来控制模块的显示呢?

阅读 9.8k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题