vue-动态切换组件

vue-动态切换组件

一.需求

  日常项目中需要动态去切换组件进行页面展示。
  例如:登陆用户是“管理员”或者“普通用户”,需要根据登陆的用户角色切换页面展示的内容. 则需要使用 :is 属性进行绑定切换

user_type:

  • admin
  • editor

二.实现

index.vue

<template>
  <div class="dashboard-container">
    <component :is="currentRole" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import adminDashboard from './admin'
import editorDashboard from './editor'

export default {
  name: 'Dashboard',
  components: { adminDashboard, editorDashboard },
  data() {
    return {
      // 默认admin
      currentRole: 'adminDashboard'
    }
  },
  computed: {
    ...mapGetters([
      'roles'
    ])
  },
  created() {
    // 判断是否为超管
    if (!this.roles.includes('admin')) {
      // 切换为 editor
      this.currentRole = 'editorDashboard'
    }
  }
}
</script>

有收获?希望老铁们来个三连击,给更多的同学看到这篇文章

发布了10 篇原创文章 · 获赞 18 · 访问量 4137

猜你喜欢

转载自blog.csdn.net/weixin_38150130/article/details/103935258