• Vite Vue3+Element Plus框架布局


    App根组件:框架布局

    1. <template>
    2. <el-container class="layout-container-demo" style="height: 98vh">
    3. <el-aside width="200px">
    4. <el-scrollbar>
    5. <el-menu :router="true" :default-active="route.fullPath">
    6. <el-menu-item index="/home">
    7. <el-icon>
    8. <HomeFilled />
    9. el-icon>
    10. <span>首页span>
    11. el-menu-item>
    12. <el-sub-menu index="/news">
    13. <template #title>
    14. <el-icon><icon-menu />el-icon>新闻管理
    15. template>
    16. <el-menu-item index="/news/add">新增新闻el-menu-item>
    17. <el-menu-item index="/news/list">新闻列表el-menu-item>
    18. el-sub-menu>
    19. el-menu>
    20. el-scrollbar>
    21. el-aside>
    22. <el-container>
    23. <el-header style="text-align: right; font-size: 12px">
    24. <div>新闻管理系统div>
    25. <div>欢迎Lily回来div>
    26. el-header>
    27. <el-main>
    28. <el-scrollbar>
    29. <RouterView>RouterView>
    30. el-scrollbar>
    31. el-main>
    32. el-container>
    33. el-container>
    34. template>
    35. <script setup>
    36. import { ref } from 'vue'
    37. // 注册icon :icon需要单独注册
    38. import { Menu as IconMenu, Message, Setting, HomeFilled } from '@element-plus/icons-vue'
    39. import { useRouter, useRoute } from 'vue-router'
    40. const router = useRouter();
    41. const route = useRoute();
    42. script>
    43. <style>
    44. * {
    45. margin: 0;
    46. padding: 0;
    47. }
    48. .el-header {
    49. background: #f5f7fa;
    50. height: 80px;
    51. width: 100%;
    52. line-height: 80px;
    53. display: flex;
    54. justify-content: space-between;
    55. align-items: center;
    56. /*字体垂直方向居中*/
    57. }
    58. .el-header div {
    59. font-size: 16px;
    60. }
    61. style>

    AddNews.vue 新增新闻

    1. <template>
    2. <el-form :model="news" label-width="120px">
    3. <el-form-item label="标题">
    4. <el-input v-model="news.title" />
    5. el-form-item>
    6. <el-form-item label="类型">
    7. <el-select v-model="news.type" placeholder="类型">
    8. <el-option label="军事" value="军事" />
    9. <el-option label="娱乐" value="娱乐" />
    10. <el-option label="财经" value="财经" />
    11. el-select>
    12. el-form-item>
    13. <el-form-item label="内容">
    14. <el-input v-model="news.desc" type="textarea" />
    15. el-form-item>
    16. <el-form-item>
    17. <el-button type="primary" @click="onAddNews">创建el-button>
    18. <el-button>取消el-button>
    19. el-form-item>
    20. el-form>
    21. template>
    22. <script lang="ts" setup>
    23. import {ref, reactive } from 'vue'
    24. import newsStore from '../stores/newsStore'
    25. const store=newsStore();
    26. const news=ref({
    27. title:"",
    28. type:"",
    29. desc:"",
    30. })
    31. const onAddNews = () => {
    32. store.addNews(news.value); //新增新闻
    33. }
    34. script>

    NewsList.vue 新闻列表

    1. <template>
    2. <el-table :data="store.newsList" style="width: 100%">
    3. <el-table-column fixed prop="title" label="标题" />
    4. <el-table-column prop="type" label="分类" />
    5. <el-table-column prop="desc" label="内容" />
    6. <el-table-column>
    7. <template #default="scope">
    8. <el-button link type="primary" size="small" @click="handleClick(scope.row)">删除el-button>
    9. <el-button link type="primary" size="small">编辑el-button>
    10. template>
    11. el-table-column>
    12. el-table>
    13. template>
    14. <script setup>
    15. import newsStore from '../stores/newsStore'
    16. const store = newsStore();
    17. console.log(store.newsList)
    18. const handleClick = (row) => {
    19. store.deleteNews(row.title)
    20. }
    21. script>

    /router/index 路由

    1. import { createRouter, createWebHistory } from "vue-router"; //导入vue-router路由模块,createWebHashHistor函数
    2. const routes = [
    3. {
    4. path: "/", //路径:
    5. redirect: "/Home" //涉及到多级页面跳转需要用路径的跳转,不能用name的跳转; 浏览器进入http://localhost:5173/ 首先要跳转的路径是/Films,即:要跳转到http://localhost:5173/Films,而进入http://localhost:5173/Films后又发现/Films要重定向到/Films/NowPlaying,这样就实现了打开http://localhost:5173/就加载出来了http://localhost:5173/Films/NowPlaying内容
    6. // redirect: {
    7. // name: "Films" //重定向到路由名称为Tabbar的路由中,这样当浏览器输入的是:http://localhost:5173/ 则会重定向跳转到 http://localhost:5173/Films
    8. // }
    9. },
    10. {
    11. path: "/home", //路径:导航栏
    12. name: "Home",
    13. //当路由被触发时,该组件才会被异步加载,举列:打开页面就不会加载所有的组件,而是根据当前页面需要的组件进行异步加载
    14. //这样可以减少初始加载时间,提升用户体验,同时也节省了不必要的资源消耗。
    15. component: () => import("../views/Home.vue")
    16. },
    17. {
    18. path: "/news/add", //路径:底部选项卡
    19. name: "AddNews", //路由名称,如果不指定name 默认的name为default
    20. //当路由被触发时,该组件才会被异步加载,举列:打开页面就不会加载所有的组件,而是根据当前页面需要的组件进行异步加载
    21. //这样可以减少初始加载时间,提升用户体验,同时也节省了不必要的资源消耗。
    22. component: () => import("../views/AddNews.vue"),
    23. },
    24. {
    25. path: "/news/list", //路径:底部选项卡
    26. name: "NewsList", //路由名称,如果不指定name 默认的name为default
    27. //当路由被触发时,该组件才会被异步加载,举列:打开页面就不会加载所有的组件,而是根据当前页面需要的组件进行异步加载
    28. //这样可以减少初始加载时间,提升用户体验,同时也节省了不必要的资源消耗。
    29. component: () => import("../views/NewsList.vue"),
    30. },
    31. {
    32. path: "/:pathMatch(.*)", //404错误
    33. name: "NotFound", //路由名称,如果不指定name 默认的name为default
    34. component: () => import("../views/NotFound.vue")
    35. }
    36. ]
    37. //创建路由对象
    38. const router = createRouter({
    39. history: createWebHistory(), //这种方式基于浏览器 history API 的路由模式,url的样式是:http://localhost:5173/list
    40. routes: routes,
    41. })
    42. //路由全局拦截:在进入页面之前就进行拦截。可以用于做用户登陆验证
    43. //参数to: 表示即将进入的目标路由对象
    44. //参数from:表示当前导航正要离开的路由
    45. //参数next:调用该方法后才能进入下一个钩子。next() 直接进入下一个钩子,next(false) 中断当前的导航。next('/') 或者 next({ path: '/' }) 则会进入一个不同的地址。
    46. router.beforeEach(async (to, from, next) => {
    47. const isAuthenticated = await localStorage.getItem('token');
    48. console.log(to.fullPath); //全路径
    49. console.log(to.path); //路径
    50. console.log(to.name); //路由名称
    51. console.log(to.params); //路由参数:http://localhost:5173/FilmsDetail/123
    52. console.log(to.query); //路由参数:http://localhost:5173/FilmsDetail?myid=123
    53. console.log(to.meta); //路由自定义参数
    54. //meta.requireAuth表示当前请求的页面需要验证, 并且未登录
    55. if (to.meta.requireAuth && !isAuthenticated) {
    56. next(`/login?redirect=${to.path}`) //to.fullPath
    57. }
    58. else next() //如果不是请求的登陆界面,或者已经登陆过了,则直接跳转到用户请求的界面
    59. })
    60. //路由全局拦截:在进入页面之后才进行触发拦截。
    61. router.afterEach(async (to, form) => {
    62. //用的比较少,一般用于收集一些日志信息,做用户行为分析:例如:收集页面浏览量:PV
    63. })
    64. export default router //导出router路由对象//导出router路由对象

    /stores/newsStore.js  状态管理器

    1. import { defineStore } from 'pinia'
    2. import axios from 'axios'
    3. import { computed, ref } from 'vue'
    4. const newsStore = defineStore("newsStoreId", () => {
    5. const newsList = ref([]);
    6. //新增新闻
    7. const addNews = (news) => {
    8. newsList.value.push({...news}) //{...news} 展开news 将news作为一个新的对象添加到newsList中,否则只是将原先news对象的一个引用给加进去了,现在修改了原先的也将修改
    9. }
    10. const deleteNews = (title) => {
    11. newsList.value = newsList.value.filter(news => news.title !== title);
    12. }
    13. return {
    14. newsList,
    15. addNews,
    16. deleteNews
    17. }
    18. })
    19. export default newsStore;

    vite.config.js 配置文件

    1. import { defineConfig } from 'vite'
    2. import vue from '@vitejs/plugin-vue'
    3. import AutoImport from 'unplugin-auto-import/vite'
    4. import Components from 'unplugin-vue-components/vite'
    5. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
    6. // 自动导入Icon图标
    7. import IconResolver from "unplugin-icons/resolver";
    8. import Icons from "unplugin-icons/vite";
    9. // import 'element-plus/es/components/button/style/css'
    10. // https://vitejs.dev/config/
    11. export default defineConfig({
    12. plugins: [
    13. vue(),
    14. // 按需引入Element-plus //引入icon
    15. AutoImport({
    16. resolvers: [
    17. ElementPlusResolver({ importStyle: false }), // 组件自动导入
    18. IconResolver({ prefix: "icon" }),
    19. ],
    20. }),
    21. Components({
    22. resolvers: [
    23. ElementPlusResolver(),// 组件自动导入
    24. IconResolver({
    25. //prefix: 'icon', // 修改Icon组件前缀,不设置则默认为i,禁用则设置为false
    26. enabledCollections: ["ep"] // 指定collection,即指定为elementplus图标集ep
    27. }),
    28. ],
    29. }),
    30. Icons({ scale: 1, defaultClass: "inline-block", autoInstall: true }),
    31. ],
    32. })

    main.js 注册器

    1. import { createApp } from 'vue'
    2. import './style.css'
    3. import App from './App.vue'
    4. import router from "../src/router/index.js" //导入路由js
    5. import { createPinia} from 'pinia' //导入状态管理器js
    6. const pinia = createPinia();
    7. const app = createApp(App);
    8. app.use(pinia);
    9. app.use(router);
    10. app.mount('#app')

  • 相关阅读:
    linux http代理设置
    【工作中遇到的性能优化问题】
    LeetCode每日一题(2348. Number of Zero-Filled Subarrays)
    uniapp nvue 踩坑记录
    【002】数组少不了
    使用docker 注册runner
    企业电子招标采购系统源码Spring Boot + Mybatis + Redis + Layui + 前后端分离 构建企业电子招采平台之立项流程图
    【重识云原生】第六章容器6.2.2节——K8S架构剖析
    2022 阿里全球数学竞赛获奖名单公布,其中 00 后选手占了一半多,如何评价这一现象?
    搭建Python开发环境
  • 原文地址:https://blog.csdn.net/Fanbin168/article/details/134465434