当前位置:   article > 正文

vite+vue3使用element-plus_vue3 vite elementplus

vue3 vite elementplus

  • 代码目录 

  • 组件库
npm install element-plus --save
  • 图标库
npm install @element-plus/icons-vue
  • main.js导入组件
  1. import { createApp } from 'vue'
  2. import App from './App.vue'
  3. import router from "@/router/index"
  4. import { store, key } from "@/store/index"
  5. import ElementPlus from 'element-plus'
  6. import * as Icons from '@element-plus/icons-vue'
  7. import 'element-plus/dist/index.css'
  8. //全局引入css
  9. import '@/assets/glob.css'
  10. const app = createApp(App)
  11. app.use(ElementPlus)
  12. app.use(router)
  13. app.use(store, key)
  14. app.mount('#app')
  15. //动态图标库
  16. Object.keys(Icons).forEach((key) => {
  17. // console.log(key);
  18. app.component(key,Icons[key as keyof typeof Icons])
  19. })
  • 使用element plus
<el-container>
  1. 完整代码
  1. <template>
  2. <div class="common-layout">
  3. <el-container>
  4. <!--侧边栏-->
  5. <el-aside :width="isCollapse ? '64px' : '200px'">
  6. <MenuBar :menuList="menuList"></MenuBar>
  7. </el-aside>
  8. <el-container>
  9. <!--头部-->
  10. <el-header>
  11. <Header></Header>
  12. </el-header>
  13. <!--右侧主体-->
  14. <el-main>
  15. <TabBar></TabBar>
  16. </el-main>
  17. </el-container>
  18. </el-container>
  19. </div>
  20. </template>
  21. <script lang="ts" setup true>
  22. import { computed, reactive, ref } from 'vue';
  23. import { useRouter } from 'vue-router';
  24. import { useStore } from '@/store';
  25. import { menusAPI } from '@/utils/api'
  26. import Header from '@/components/layout/Header.vue';
  27. import MenuBar from '@/components/layout/MenuBar.vue';
  28. import TabBar from '@/components/layout/TabBar.vue';
  29. let router = useRouter()
  30. let store = useStore()
  31. const isCollapse = computed(() => {
  32. return store.getters.isCollapse
  33. })
  34. const menuList = reactive([] as any)
  35. function goHome() {
  36. router.push("/login")
  37. }
  38. getMenu()
  39. function getMenu() {
  40. menusAPI().then(({ data }) => {
  41. console.info(data)
  42. if(data.code==500){
  43. router.push("/login")
  44. }
  45. if (data.menus && data.menus.length > 0) {
  46. data.menus.forEach((element: any) => {
  47. menuList.push(element)
  48. });
  49. }
  50. })
  51. }
  52. </script>
  53. <style lang="scss" scoped>
  54. .el-aside {
  55. margin-right: 3px;
  56. border-radius: 5px;
  57. border: 1px solid #dddfe6;
  58. }
  59. .el-header {
  60. display: flex;
  61. align-items: center;
  62. color: #000000;
  63. border-radius: 5px;
  64. margin-bottom: 3px;
  65. border: 1px solid #dddfe6;
  66. }
  67. .el-main {
  68. min-height: 100vh;
  69. border-radius: 5px;
  70. border: 1px solid #dddfe6;
  71. --el-main-padding:0px;
  72. }
  73. .el-tabs__nav-scroll{
  74. height: 10px;
  75. }
  76. </style>
  • 使用element icon
  1. <component class="icon" @click="toggleCollapse"
  2. :is="isCollapse ? Expand : Fold" />
  1. 完整代码 
  1. <template>
  2. <component class="icon" @click="toggleCollapse" :is="isCollapse ? Expand : Fold" />
  3. <el-breadcrumb separator="/" style="margin-left: 35px;">
  4. <el-breadcrumb-item v-for="item in tab" >{{ item.name }}</el-breadcrumb-item>
  5. </el-breadcrumb>
  6. </template>
  7. <script lang="ts" setup true>
  8. import { Ref, computed, ref, watch } from 'vue';
  9. import { useRouter, useRoute,RouteLocationMatched } from 'vue-router';
  10. import { useStore } from '@/store';
  11. import { Expand, Fold } from '@element-plus/icons-vue';
  12. const store = useStore();
  13. const router = useRouter();
  14. const route = useRoute();
  15. let userName = ref(store.getters.name)
  16. const tab:Ref<RouteLocationMatched[]>=ref([])
  17. //退出
  18. function logout() {
  19. router.push("/login")
  20. sessionStorage.clear()
  21. }
  22. const isCollapse = computed(() => {
  23. return store.getters.isCollapse
  24. })
  25. //点击按钮切换菜单的折叠和展开
  26. function toggleCollapse() {
  27. store.commit('SET_COLLAPSE', !store.getters.isCollapse)
  28. }
  29. const getBredcurm = () => {
  30. let mached = route.matched.filter((item: any) => item)
  31. tab.value=mached;
  32. }
  33. getBredcurm()
  34. watch(()=>route.path,()=>getBredcurm())
  35. </script>
  36. <style lang="scss" scoped>
  37. .icon {
  38. width: 32px;
  39. height: 18px;
  40. }
  41. .icon:hover {
  42. cursor: pointer;
  43. }
  44. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/225674
推荐阅读
相关标签
  

闽ICP备14008679号