当前位置:网站首页>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
边栏推荐
- 盘点华为云GaussDB(for Redis)六大秒级能力
- 【无标题】第一次参加csdn活动
- Is machine learning suitable for girls?
- 1-20 pre inspection request
- 用yml文件进行conda迁移环境时的报错小结
- 1-12 preliminary understanding of Express
- Which direction should college students choose to find jobs after graduation?
- 1-19 using CORS to solve interface cross domain problems
- 1-13 express监听GET和POST请求&处理请求
- Reading notes of Clickhouse principle analysis and Application Practice (1)
猜你喜欢

吴恩达的机器学习适合入门吗?

Pytorch quantitative practice (2)

Jupyter notebook/lab switch CONDA environment

布隆过滤器

Anaconda下安装Jupyter notebook

阿婆做的臭豆腐

《Dynamic Routing Between Capsules》论文学习总结

Five years after graduation, I wondered if I would still be so anxious if I hadn't taken the test

AKK菌——下一代有益菌

Arcmap|assign values to different categories of IDS with the field calculator
随机推荐
Clickhouse distributed table engine
The Jenkins download Plug-in can't be downloaded. Solution
Troubleshooting the problem of pytorch geometric torch scatter and torch spark installation errors
Anaconda下安装Jupyter notebook
Windbg调试工具介绍
Deployment and use of Nacos
Qsort function and Simulation Implementation of qsort function
1. Summary of wechat applet page Jump methods; 2. the navigateto stack does not jump to the tenth floor
请问,启牛证券开户,可以开户吗?安全吗?你想要的答案全在这里
[backtracking] full arrangement leetcode46
国产数据库乱象
Five years after graduation, I wondered if I would still be so anxious if I hadn't taken the test
【回溯】全排列 II leetcode47
1-17 express Middleware
兴奋神经递质——谷氨酸与大脑健康
qsort函数和模拟实现qsort函数
AKK菌——下一代有益菌
Summary of errors reported when using YML file to migrate CONDA environment
Sqlserver gets the data of numbers, Chinese and characters in the string
Develop your own package
