首页 > 编程知识 正文

vue开发,vue动态路由怎么实现

时间:2023-05-03 14:02:36 阅读:146062 作者:1542

坐代码吧

在我们的router.js文件下

/* * @Description:路由设置*/import Vue from 'vue; 导入路由器自' vue-router '; 导入存储从' . /存储/存储'; //要使用的vuex--store//软件包的路由已部署并导入nd from './nd '; 导入q From './q '; //按钮权限import { getcurrentmodulebtnpermission } from ' ./utils/BTN permission '//按钮权限操作vue.use (路由器); 解决未通过Vue-Router升级捕获的导航uncaught(inpromise ) navigation guard问题constoriginalpush=router.prototype.pupure 根原型的push router.prototype.push=function push (location,onResolve,onReject ) {if ) onresolve||onreject ) rettion 使用on reject (returnoriginalpush.call (this,location ).catch(err ) )//引入的根const routes=[.nd, err] //路由侦听操作router.beforeeach () )、from、 确定是否需要登录next ()//if (to.matched.some ) RES=RES.meta.auth ) )/如果匹配信息,则可以直接登录到if(sessionstorage('username ) ) (if (to.matched.some (RES=RES.meta.BTN ) ) /权限按钮操作getcural commit事件store.commit('sethistory ',to ); next (; } else {//如果没有权限btn sessionStorage.clear (; //清除存储信息,登录页面next(path:(/login )、query: ) redirect:to.fullpath(/重定向) ); } } else { //信息store.comMIT('sethistory ',to ); (下一个(} )导出默认路由器() ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )

然后继续。

在store文件夹下创建store.js文件

importvuefrom ' vue ' importvuexfrom ' vuex ' import { set token } from ' @/utils/storage.js ' vue.use (vuex ) constststs //token Loading: false,//loading控件index 3360 local storage.getitem (' index ' ) )。 //底部导航状态lang: 'cn ',//语言history: [] },//异步操作mutations3360{set_token(state,token ) state.id },isindex(state,n ) localstorage.setitem ) ' index ',n ); state.index=n; },setlang(state,lang ) (localstorage.setitem ),lang ); state.lang=lang;sethistory(state,n ) )//过滤相同数据的let arr=state.history; let isa=arr.filter (item=item.path==n.path ); if(isa.Length==0)//判断是否为欢迎页面if (n.path!='/welcome ' n.meta.name (state.history.push (n ) ); }、reducehistory(state,n ) /删除记录let arr=state.history; let索引; ARR.foreach((item,I ) ) if ) item.path==n.path ) { index=i; } ) ARR.splice(index,1 ); },allhistory(state )//所有会话存储. remove item (' history ); state.history=[]; },otherhistory(state,n ) /其他if ) state.history.length ) { state.history=[]; state.history.push(n; },//同步操作actions: { setToken({ commit },token } { return new promise ((resolve,reject )={commit ) ) set _ to misto },editindex(context,n ) ) context.comMIT )、isindex )、n ); }}; 在export default store中,您将继续编写在后端合作的按钮权限文件

导入http from ' ./plugin/axios '; letapi={ BTN permission : ' /网关/能源-服务- UC enter/API/roles/BTN/permission ', //获取按钮权限}/** *按钮权限判断* @ param value * @ returns { boolean } */exportfunctioncheckbtnpermission (value ) () ) ) ) 652 { const permissions=session storage.getitem }返回权限. index of (value )!=-1 } exportfunctiongetcurrentmodulebtnpermission (数据) returnhttp.gets ) API.btnpermission,{ ' path ' :数据}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。