当前位置:网站首页>uniapp-路由uni-simple-router
uniapp-路由uni-simple-router
2022-06-30 20:20:00 【智江鹏】
背景
- 专为uniapp打造的路由器,和uniapp深度集成
- 通配小程序、App和H5端
- H5能完全使用vue-router开发
- 模块化、查询、通配符、路由参数
- 使 uni-app实现嵌套路由(仅H5端完全使用vue-router)
- uniapp用到了很多vue的api,但在路由管理的功能相对于vue-router还是比较欠缺的,比如全局导航守卫
安装
// 项目根目录执行命令行
npm install uni-simple-router
// 根据pages.json总的页面,自动构建路由表
npm install uni-read-pages
配置vue.config.js
注:如果根目录没有vue.config.js文件,请手动创建
// vue.config.js
const TransformPages = require('uni-read-pages')
const { webpack } = new TransformPages()
module.exports = {
configureWebpack: {
plugins: [
new webpack.DefinePlugin({
ROUTES: webpack.DefinePlugin.runtimeValue(() => {
const tfPages = new TransformPages({
includes: ['path', 'name', 'aliasPath','meta']
});
return JSON.stringify(tfPages.routes)
}, true)
})
]
}
}
新建router.js文件
import { RouterMount, createRouter } from 'uni-simple-router';
const router = createRouter({
platform: process.env.VUE_APP_PLATFORM,
routes: [...ROUTES]
});
//全局路由前置守卫
router.beforeEach((to, from, next) => {
//权限控制登录
if(to.meta.auth){
console.log("需要登录");
if("token"){
next();
}else{
console.log("请登录");
}
}else{
console.log("不需要登录");
next();
}
console.log("前置守卫"+JSON.stringify(to));
});
// 全局路由后置守卫
router.afterEach((to, from) => {
console.log('跳转结束')
})
export {
router,
RouterMount
}
main.js
import {router,RouterMount} from './router/router.js' //路径换成自己的
Vue.use(router)
//v1.3.5起 H5端 你应该去除原有的app.$mount();使用路由自带的渲染方式
// #ifdef H5
RouterMount(app,router,'#app')
// #endif
// #ifndef H5
app.$mount(); //为了兼容小程序及app端必须这样写才有效果
// #endifpage.json
{
"pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
{
"path": "pages/index/index",
"name": "index",
"style": {
"navigationBarTitleText": "uni-app"
}
}, {
"path": "pages/home/home",
"name": "home",
"meta": {
"auth": false, //需要登录
"async": true, //是否同步
"title": "首页", //标题
"group": "商城" //分组
},
"style": {
"navigationBarTitleText": "",
"enablePullDownRefresh": false
}
},{
"path": "pages/haha/haha",
"name": "haha",
"meta": {
"auth": true, //需要登录
"async": true, //是否同步
"title": "首页", //标题
"group": "商城" //分组
},
"style": {
"navigationBarTitleText": "",
"enablePullDownRefresh": false
}
}
],
"globalStyle": {
"navigationBarTextStyle": "black",
"navigationBarTitleText": "uni-app",
"navigationBarBackgroundColor": "#F8F8F8",
"backgroundColor": "#F8F8F8"
}
}
页面跳转和参数接收
- push()
- pushTab() : 跳转tar栏
- replace() : 替换
- replaceAll() : 替换所有
- back() : 直接带上数字返回第几层
- 注意:path和query配合使用,而name和params配合使用
//通过name方式跳转
this.$Router.push({
name: 'home',
params: {
name: 'Joseph',
age: 22
}
})
------------------------------------
//通过path形式进行跳转
this.$Router.push({
path: '/pages/haha/haha',
query: {
name: 'Josdep33333h',
age: 24
}
})
-------------------------------------
//用uni形式跳转到新页面,并传递参数
uni.navigateTo({
url:'/pages/home/home?id=2&name=Josep33333h'
});// 页面接收参数——query中的参数
onLoad(){
// query传参
const query=this.$Route.query
// params传参
const params=this.$Route.params
}组件
// 首先在main.js中进行注册,将my-link注册为全局组件,注册后使用方法同<router-link>
import Mylink from './node_modules/uni-simple-router/dist/link.vue'
Vue.component('my-link',Mylink)
// 使用
// navType对应的就是push/pushTab/replace/replaceAll
<my-link to="{path: '/pages/mine/index',query: {name: '我只想去tab5的my-link'}}" navType="pushTab">
<button type="default">我是router-link</button>
</my-link>
onLoad(option) {
//原生获取数据
console.log("zz",option);
// query传参
const query=this.$Route.query
console.log(query);
// params传参
const params=this.$Route.params
console.log(params);
}详情使用参考
边栏推荐
- mysql登录出现1045错误修改方法[通俗易懂]
- Wechat applet development practice cloud music
- 杰理之检测灵敏度级别确定【篇】
- Lumiprobe copper free click chemical solution
- 杰理之触摸按键识别流程【篇】
- B_QuRT_User_Guide(33)
- Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
- 股票开户优惠链接,我如何才能得到?另外,手机开户安全么?
- Lumiprobe 改性三磷酸盐丨生物素-11-UTP研究
- Openfire在使用MySQL数据库后的中文乱码问题解决
猜你喜欢

左值引用和右值引用

《大厂面试》之JVM篇21问与答

【数字IC应届生职业规划】Chap.1 IC行业产业链概述及代表企业大厂汇总

基于开源流批一体数据同步引擎ChunJun数据还原—DDL解析模块的实战分享

Solve the problems of Devops landing in complex environment with various tools with full stack and full function solutions

Basic components of STL

B_QuRT_User_Guide(32)

DEX文件解析 - method_ids解析

Lumiprobe蛋白质定量丨QuDye 蛋白定量试剂盒
![翻转链表II[翻转链表3种方式+dummyHead/头插法/尾插法]](/img/a8/6472e2051a295f5e42a88d64199517.png)
翻转链表II[翻转链表3种方式+dummyHead/头插法/尾插法]
随机推荐
Peking University ACM problems 1003:hangover
Analysis of breakpoint continuation and download principle
Maya House Modeling
北京大学ACM Problems 1001:Exponentiation
19.04 分配器
Black apple server system installation tutorial, black apple installation tutorial, teach you how to install black apple in detail [easy to understand]
By analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed by the industry
翻转链表II[翻转链表3种方式+dummyHead/头插法/尾插法]
树基本概念
obsidian配合hugo的使用,让markdown本地编辑软件与在线化无缝衔接
Jenkins打包拉取不到最新的jar包
How do I get the largest K massive data
Dynamic style binding --style and class
北京大学ACM Problems 1004:Financial Management
北京大学ACM Problems 1006:Biorhythms
Heartbeat and DRBD configuration process
Huffman Tree (1) Basic Concept and C - language Implementation
Notes on modification of Jerry's test box pairing software [chapter]
DEX文件解析 - method_ids解析
PHP obtains opcode and C source code
