当前位置: 首页 > news >正文

vue + element-plus 开发中遇到的问题

1.问题之路由守卫

      初写路由守卫,对于next()的理解不是很透彻,就想着都放行,不然看不到效果,结果控制台出现了警告,想着报黄的问题就不是问题,但仔细一看发现他说,如果再生产阶段就会失败,就觉得有必要解决一下

 以下是我的代码

//路由鉴权
import router from "@/router"import nprogress from "nprogress"
//引入进度条的样式
import "nprogress/nprogress.css"//删除加载进度条傍边的小球
nprogress.configure({showSpinner:false})
//引入小仓库
import { store } from "@/stores/modules/user"
import pinia from "./stores" //在外部使用小仓库之前的引入大仓库,否则会报错,可以理解为仓库时通过大仓库的create而建立,没有引入大仓库,小仓库就不存在,而组件内是因为在main.ts中通过use()使用了
let useStore = store(pinia) //然后将pinia传进去
//console.log(useStore)
//全局守卫:项目当中任意路由切换都会触发的钩子
//全局前置首位router.beforeEach(async (to: any, from: any, next: any) => {//浏览器任务栏的名字document.title = to.meta.title//to:你将要访问的路由对象//from:你从哪个路由而来//next:路由的放行函数//console.log(nprogress)nprogress.start();let token = useStore.token;//获取用户信息let userinfo = useStore.netName//通过token来鉴别是否登录成功if (token) {//登录成功,不能访问login,指向首页if (to.path == "/login") {next({ path: '/' })}//登录成功访问其余几个路由else {//有用户信息if (userinfo) {//放行next();} else {//如果没有用户信息,先获取用户信息再放行try {//获取用户信息await useStore.userInfo()//放行next();} catch (error) {//token过期:获取不到用户信息//用户手动修改了本地储存的token//退出登录 用户相关的数据清空useStore.userOutLogin();next({ path: '/login', query: { redirect: to.path } })}}}} else {if (to.path == '/login') {next();} else {next({ path: '/login', query: { redirect: to.path } }) //返回登陆页面,并在地址栏中添加你想去的页面,登录成功后可以直接去}}//问题所在next();
})
//全局后置首位
router.afterEach(() => {nprogress.done();})

问题所在我特意加了很多空行,报黄的原因是路由导航执行了两次next函数,只要把最下面的next函数善删了就OK了

2.问题之el-menu

        这个问题是我纯属眼瞎

        以下是代码

<script setup lang='ts'>
import { ref, onMounted } from "vue"
import Menu from "../menu/test.vue"
//引入路由
import { useRouter } from "vue-router";//获取路由器对象
let router = useRouter();
//接收路由数组
defineProps({menuList: Object,
})const goRouter = (item: any) => {//路由跳转router.push(item.index)
}
</script><template><div class="menu-container"><template v-for="(item, index) in menuList"><template v-if="!(item.meta as any).hidden"><!--  无子路由  --><el-menu-item :index="item.path" v-if="!item.children" @click="goRouter"><template #title>  <el-icon><component :is="(item.meta as any).icon"></component></el-icon><span>{{ (item.meta as any).title }}</span></template></el-menu-item><!--  仅一个子路由  --><el-menu-item :index="item.path" v-if="item.children && item.children.length == 1" @click="goRouter"><template #title><el-icon><component :is="(item.children[0].meta as any).icon"></component></el-icon><span>{{ (item.children[0].meta as any).title }}</span></template></el-menu-item><!--  一子路由以上  --><el-sub-menu v-if="item.children && item.children.length > 1" :index="item.children[0].path"><template #title><el-icon><component :is="(item.meta as any).icon"></component></el-icon><span>{{ (item.meta as any).title }}</span></template><Menu :menuList="item.children"></Menu></el-sub-menu></template></template></div>
</template><style lang="less" scoped></style>

最后的页面,(折叠后的菜单)

预期的样子

 产生的原因:将图标放到插槽title里面,导致图标和标题一起折叠,el-sub-menu与el-menu-item有些不一样,他的图标是放在插槽里的,也正是这个原因使搞混了,看文档一定要仔细,我因为这问题耽误了有近两个小时(还有一点是因为我不爱看文档,这毛病的改了)

解决:将图标放到外面

如:

 <el-menu-item :index="item.path" v-if="!item.children" @click="goRouter"><el-icon><component :is="(item.meta as any).icon"></component></el-icon><template #title><span>{{ (item.meta as any).title }}</span></template>
</el-menu-item>

3.问题之内容区跳转

 一般做某某管理系统,点击不同的菜单,内容区跳转到不同的内容路由,所以要跳转路由的一路由必须为主页的路由

如:

  {path: '/',component: () => import('@/views/main/index.vue'),  //这个路由为由组件拼成的主页面name: '/',meta: {title: "",hidden: false,icon:'',},redirect:'/home',children:[{path:'/home',component: ()=> import('@/views/home/index.vue'),name:'home',meta:{title: "首页",hidden: false,icon:'House'}} ]}
,
{path: '/goodsMange',component: () => import('@/views/main/index.vue'),  //第一次写的时候写了其他新建的文件一整个页面都跳了name:'goodsMange',meta:{title:'商品管理',hidden:false,icon:'Goods',},redirect: '/goodsMange/SKU',children: [{path: '/goodsMange/SKU',component: ()=> import('@/views/goodsManage/SKU/index.vue'),name:'sku',meta:{title:'SKU',hidden:false,icon:'Goods',},},{path: '/goodsMange/SPU',component: ()=> import('@/views/goodsManage/SPU/index.vue'),name:'spu',meta:{title:'SPU',hidden:false,icon:'Goods',},}]},

http://www.lryc.cn/news/348789.html

相关文章:

  • 使用java远程提交flink任务到yarn集群
  • 麻了!新增4.1分,CCF-C类,2区毕业神刊,被标记On Hold!
  • tomcat 的启动流程
  • YOLOv9全网最新改进系列::YOLOv9完美融合双卷积核(DualConv)来构建轻量级深度神经网络,目标检测模型有效涨点神器!!!
  • PCIE协议-2-事务层规范-MEM/IO/CFG request rules
  • jmeter分布式集群压测
  • 美国加州正测试ChatGPT等生成式AI,在4大部门应用
  • 【Kali Linux工具篇】wpscan的基本介绍与使用
  • C#算法之计数排序
  • EasyExcel简单使用
  • Notes客户端中的漫游功能
  • 为什么要内存对齐?
  • 23、Flink 的 Savepoints 详解
  • 【Unity】Unity项目转抖音小游戏(二)云数据库和云函数
  • SpringBoot集成jasypt对yml文件指定参数加密并自定义@bean隐藏密钥
  • GDB的使用
  • Linux处理用户输入
  • 【代码笔记】高并发场景下问题解决思路
  • 【Docker系列】Linux部署Docker Compose
  • 基于SSM的文化遗产的保护与旅游开发系统(有报告)。Javaee项目。ssm项目。
  • 整合springboot-mybatis时,MySQL数据库无法连接问题
  • C语言循环队列
  • Docker运行出现iptables: No chain/target/match by that name报错如何解决?
  • 力扣 122. 买卖股票的最佳时机 II python AC
  • F5 BIG-IP Next Central Manager SQL注入漏洞(CVE-2024-26026、CVE-2024-21793)
  • Python3 笔记:循环结构 for语句
  • 信息化与数字化的区别在哪里?
  • 记录MySQL数据库查询不等于xxx时的坑
  • QT的创建,发现编译器有一个黄色三角形感叹号,提示说Cmake配置错误,该怎么办?
  • 0506libMaven项目