uniapp-路由uni-simple-router安裝配置教程
背景
- 專為uniapp打造的路由器,和uniapp深度集成
- 通配小程序、App和H5端
- H5能完全使用vue-router開(kāi)發(fā)
- 模塊化、查詢、通配符、路由參數(shù)
- 使 uni-app實(shí)現(xiàn)嵌套路由(僅H5端完全使用vue-router)
- uniapp用到了很多vue的api,但在路由管理的功能相對(duì)于vue-router還是比較欠缺的,比如全局導(dǎo)航守衛(wèi)
安裝
// 項(xiàng)目根目錄執(zhí)行命令行 npm install uni-simple-router // 根據(jù)pages.json總的頁(yè)面,自動(dòng)構(gòu)建路由表 npm install uni-read-pages
配置vue.config.js
注:如果根目錄沒(méi)有vue.config.js文件,請(qǐng)手動(dòng)創(chuàng)建
// 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) }) ] } }
import { RouterMount, createRouter } from 'uni-simple-router'; const router = createRouter({ platform: process.env.VUE_APP_PLATFORM, routes: [...ROUTES] }); //全局路由前置守衛(wèi) router.beforeEach((to, from, next) => { //權(quán)限控制登錄 if(to.meta.auth){ console.log("需要登錄"); if("token"){ next(); }else{ console.log("請(qǐng)登錄"); } }else{ console.log("不需要登錄"); next(); } console.log("前置守衛(wèi)"+JSON.stringify(to)); }); // 全局路由后置守衛(wèi) router.afterEach((to, from) => { console.log('跳轉(zhuǎn)結(jié)束') }) export { router, RouterMount }
main.js
import {router,RouterMount} from './router/router.js' //路徑換成自己的 Vue.use(router) //v1.3.5起 H5端 你應(yīng)該去除原有的app.$mount();使用路由自帶的渲染方式 // #ifdef H5 RouterMount(app,router,'#app') // #endif // #ifndef H5 app.$mount(); //為了兼容小程序及app端必須這樣寫才有效果 // #endif
page.json
{ "pages": [ //pages數(shù)組中第一項(xiàng)表示應(yīng)用啟動(dòng)頁(yè),參考: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": "首頁(yè)", //標(biāo)題 "group": "商城" //分組 }, "style": { "navigationBarTitleText": "", "enablePullDownRefresh": false } },{ "path": "pages/haha/haha", "name": "haha", "meta": { "auth": true, //需要登錄 "async": true, //是否同步 "title": "首頁(yè)", //標(biāo)題 "group": "商城" //分組 }, "style": { "navigationBarTitleText": "", "enablePullDownRefresh": false } } ], "globalStyle": { "navigationBarTextStyle": "black", "navigationBarTitleText": "uni-app", "navigationBarBackgroundColor": "#F8F8F8", "backgroundColor": "#F8F8F8" } }
頁(yè)面跳轉(zhuǎn)和參數(shù)接收
- push()
- pushTab(): 跳轉(zhuǎn)tar欄
- replace(): 替換
- replaceAll(): 替換所有
- back(): 直接帶上數(shù)字返回第幾層
- 注意:path和query配合使用,而name和params配合使用
//通過(guò)name方式跳轉(zhuǎn) this.$Router.push({ name: 'home', params: { name: 'Joseph', age: 22 } }) ------------------------------------ //通過(guò)path形式進(jìn)行跳轉(zhuǎn) this.$Router.push({ path: '/pages/haha/haha', query: { name: 'Josdep33333h', age: 24 } }) ------------------------------------- //用uni形式跳轉(zhuǎn)到新頁(yè)面,并傳遞參數(shù) uni.navigateTo({ url:'/pages/home/home?id=2&name=Josep33333h' });
// 頁(yè)面接收參數(shù)——query中的參數(shù) onLoad(){ // query傳參 const query=this.$Route.query // params傳參 const params=this.$Route.params }
組件
// 首先在main.js中進(jìn)行注冊(cè),將my-link注冊(cè)為全局組件,注冊(cè)后使用方法同<router-link> import Mylink from './node_modules/uni-simple-router/dist/link.vue' Vue.component('my-link',Mylink) // 使用 // navType對(duì)應(yīng)的就是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) { //原生獲取數(shù)據(jù) console.log("zz",option); // query傳參 const query=this.$Route.query console.log(query); // params傳參 const params=this.$Route.params console.log(params); }
詳情使用參考
到此這篇關(guān)于uniapp-路由uni-simple-router的文章就介紹到這了,更多相關(guān)uniapp-路由uni-simple-router內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于javascript實(shí)現(xiàn)仿百度輸入框自動(dòng)匹配功能
這篇文章主要介紹了基于javascript實(shí)現(xiàn)仿百度輸入框自動(dòng)匹配功能的相關(guān)資料,需要的朋友可以參考下2016-01-01微信小程序?qū)崿F(xiàn)獲取手機(jī)號(hào)60s倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)獲取手機(jī)號(hào)60s倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07javascript html5實(shí)現(xiàn)表單驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了javascript html5實(shí)現(xiàn)表單驗(yàn)證的具體代碼,感興趣的小伙伴們可以參考一下2016-03-03ES6新特性:使用export和import實(shí)現(xiàn)模塊化詳解
本篇文章主要介紹了ES6新特性:使用export和import實(shí)現(xiàn)模塊化詳解,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07JS實(shí)現(xiàn)區(qū)分中英文并統(tǒng)計(jì)字符個(gè)數(shù)的方法示例
這篇文章主要介紹了JS實(shí)現(xiàn)區(qū)分中英文并統(tǒng)計(jì)字符個(gè)數(shù)的方法,涉及JavaScript事件響應(yīng)、正則匹配及數(shù)值運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2018-06-06前端實(shí)現(xiàn)批量下載并打包成ZIP文件的步驟及示例
下載我相信很多小伙伴并不陌生,下載文件的形式也有很多,下面這篇文章主要給大家介紹了關(guān)于前端實(shí)現(xiàn)批量下載并打包成ZIP文件的步驟及示例,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07JavaScript 函數(shù)用法詳解【函數(shù)定義、參數(shù)、綁定、作用域、閉包等】
這篇文章主要介紹了JavaScript 函數(shù)用法,結(jié)合實(shí)例形式分析了JavaScript函數(shù)定義、參數(shù)、綁定、作用域、閉包、回調(diào)函數(shù)、柯理化函數(shù)等相關(guān)概念、原理與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05基于JavaScript實(shí)現(xiàn)淘寶商品廣告效果
這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)淘寶商品廣告效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08