当前位置:网站首页>Uniapp routing uni simple router
Uniapp routing uni simple router
2022-06-30 21:57:00 【Zhijiangpeng】
background
- Specially designed for uniapp Create a new router , and uniapp Deep integration
- General configuration applet 、App and H5 End
- H5 Can fully use vue-router Development
- modularization 、 Inquire about 、 wildcard 、 Routing parameters
- send uni-app Implement nested routing ( only H5 The end is fully used vue-router)
- uniapp Used a lot of vue Of api, But the function of routing management is relative to vue-router It is still lacking , Such as global navigation guard
install
// Project root execute command line
npm install uni-simple-router
// according to pages.json The total page , Automatically build routing tables
npm install uni-read-pages
To configure vue.config.js
notes : If the root does not have vue.config.js file , Please create it manually
// 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)
})
]
}
}
newly build router.js file
import { RouterMount, createRouter } from 'uni-simple-router';
const router = createRouter({
platform: process.env.VUE_APP_PLATFORM,
routes: [...ROUTES]
});
// Global routing front guard
router.beforeEach((to, from, next) => {
// Permission control login
if(to.meta.auth){
console.log(" Need to log in ");
if("token"){
next();
}else{
console.log(" Please log in ");
}
}else{
console.log(" You don't need to log in ");
next();
}
console.log(" Front guard "+JSON.stringify(to));
});
// Global route post guard
router.afterEach((to, from) => {
console.log(' Jump end ')
})
export {
router,
RouterMount
}
main.js
import {router,RouterMount} from './router/router.js' // Change the path to your own
Vue.use(router)
//v1.3.5 rise H5 End You should remove the original app.$mount(); Use the rendering method provided by the route
// #ifdef H5
RouterMount(app,router,'#app')
// #endif
// #ifndef H5
app.$mount(); // For compatibility with applets and app The end must be written in this way to be effective
// #endifpage.json
{
"pages": [ //pages The first item in the array represents the application startup page , Reference resources :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, // Need to log in
"async": true, // Is it synchronized
"title": " home page ", // title
"group": " Shopping Mall " // grouping
},
"style": {
"navigationBarTitleText": "",
"enablePullDownRefresh": false
}
},{
"path": "pages/haha/haha",
"name": "haha",
"meta": {
"auth": true, // Need to log in
"async": true, // Is it synchronized
"title": " home page ", // title
"group": " Shopping Mall " // grouping
},
"style": {
"navigationBarTitleText": "",
"enablePullDownRefresh": false
}
}
],
"globalStyle": {
"navigationBarTextStyle": "black",
"navigationBarTitleText": "uni-app",
"navigationBarBackgroundColor": "#F8F8F8",
"backgroundColor": "#F8F8F8"
}
}
Page Jump and parameter reception
- push()
- pushTab() : Jump tar bar
- replace() : Replace
- replaceAll() : Replace all
- back() : Take the number directly and return to the layer
- Be careful :path and query In combination with , and name and params In combination with
// adopt name Way to jump
this.$Router.push({
name: 'home',
params: {
name: 'Joseph',
age: 22
}
})
------------------------------------
// adopt path Form to jump
this.$Router.push({
path: '/pages/haha/haha',
query: {
name: 'Josdep33333h',
age: 24
}
})
-------------------------------------
// use uni Form jump to new page , And pass the parameters
uni.navigateTo({
url:'/pages/home/home?id=2&name=Josep33333h'
});// Page receive parameters ——query Parameters in
onLoad(){
// query The ginseng
const query=this.$Route.query
// params The ginseng
const params=this.$Route.params
}Components
// First, in the main.js Register in , take my-link Register as a global component , Use the same method after registration <router-link>
import Mylink from './node_modules/uni-simple-router/dist/link.vue'
Vue.component('my-link',Mylink)
// Use
// navType The corresponding is push/pushTab/replace/replaceAll
<my-link to="{path: '/pages/mine/index',query: {name: ' I just want to go tab5 Of my-link'}}" navType="pushTab">
<button type="default"> I am a router-link</button>
</my-link>
onLoad(option) {
// Get data natively
console.log("zz",option);
// query The ginseng
const query=this.$Route.query
console.log(query);
// params The ginseng
const params=this.$Route.params
console.log(params);
}For details, please refer to
边栏推荐
- 从PG15 XID64再次跳票说起
- 根据肠道微生物组重新思考健康饮食
- Pytorch quantitative perception training (qat) steps
- Clickhouse native monitoring item, system table description
- Is machine learning suitable for girls?
- Troubleshooting the problem of pytorch geometric torch scatter and torch spark installation errors
- "Trust machine" empowers development
- Understand what MySQL index push down (ICP) is in one article
- 1-17 express中间件
- 1-7 Path路径模块
猜你喜欢

Neurotransmetteurs excitateurs - glutamate et santé cérébrale

Go Web 编程入门: 一探优秀测试库 GoConvey

Error reporting: internal error XFS_ WANT_ CORRUPTED_ GOTO at line 1635 of file fs/xfs/libxfs/xfs_ alloc. c.

布隆过滤器

jenkins下载插件下载不了,解决办法

Excitatory neurotransmitter glutamate and brain health

WinDbg debugging tool introduction

Look at the top 10 capabilities of alicloud cipu

牛逼|珍藏多年的工具让我实现了带薪摸鱼自由
Notes [introduction to JUC package and future]
随机推荐
On several key issues of digital transformation
《Dynamic Routing Between Capsules》论文学习总结
1-10 根据不同的url响应客户端的内容
1-15 nodemon
Summary of errors reported when using YML file to migrate CONDA environment
5G 在智慧医疗中的需求
Is it safe to open an account for stock trading on mobile phones?
The programmer's girlfriend gave me a fatigue driving test
Multi table operation - foreign key constraint
ML&DL:机器学习和深度学习中超参数优化的简介、评估指标、过拟合现象、常用的调参优化方法之详细攻略
Go Web 编程入门: 一探优秀测试库 GoConvey
Prediction and regression of stacking integrated model
Notes [introduction to JUC package and future]
Which direction should college students choose to find jobs after graduation?
Pytorch quantitative practice (2)
Understand what MySQL index push down (ICP) is in one article
Three techniques for reducing debugging time of embedded software
jupyterbook 清空控制台输出
【MySQL入门】第一话 · 初入“数据库”大陆
谈谈数字化转型的几个关键问题
