UNI-APP_uni-simple-router的快速上手(路由,nui路由拦截)
<p>官网文档:http://www.hhyang.cn/src/router/start/quickstart.html</p><p>安装插件<br>npm安装命令:npm install uni-simple-router<br>下载好后会多出这个文件夹</p>
<p><img src="https://img2020.cnblogs.com/blog/1517521/202103/1517521-20210304150510494-303089575.png"></p>
<p> </p>
<p> </p>
<p><br>初始化<br>在项目的根目录下创建如下用红框框住的文件夹及文件</p>
<p><img src="https://img2020.cnblogs.com/blog/1517521/202103/1517521-20210304150521543-207218069.png"></p>
<p> </p>
<p> </p>
<p>modules目录下的index.js内容如下(这个文件主要作用是将所有路由整合成一个数组)</p>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;">// router/modules/index.js
const files = require.context('.', false, /\.js$/)
const modules = []
files.keys().forEach(key => {
if (key === './index.js') return
const item = files(key).default
modules.push(...item)
})
export default modules
</pre>
</div>
<p> </p>
<p> </p>
<p>在modules目录下home.js文件,作用是根据不同的路由分类,添加更多模块</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> router/modules/home.js</span>
const home =<span style="color: rgba(0, 0, 0, 1)"> [
{
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注意:path必须跟pages.json中的地址对应,最前面别忘了加'/'哦</span>
path: '/pages/login/login'<span style="color: rgba(0, 0, 0, 1)">,
name: </span>'login'<span style="color: rgba(0, 0, 0, 1)">,
meta: {
title: </span>'登陆'<span style="color: rgba(0, 0, 0, 1)">,
},
}
]
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> home </pre>
</div>
<p>router下的index.js配置如下</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> router/index.js</span>
<span style="color: rgba(0, 0, 0, 1)">
import modules from </span>'./modules'<span style="color: rgba(0, 0, 0, 1)">
import Vue from </span>'vue'
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">这里仅示范npm安装方式的引入,其它方式引入请看最上面【安装】部分</span>
import Router from 'uni-simple-router'<span style="color: rgba(0, 0, 0, 1)">
Vue.use(Router)
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">初始化</span>
const router = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Router({
routes: [...modules]</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">路由表</span>
<span style="color: rgba(0, 0, 0, 1)">});
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">全局路由前置守卫</span>
router.beforeEach((to, from, next) =><span style="color: rgba(0, 0, 0, 1)"> {
console.log(</span>"全局路由前置守卫"<span style="color: rgba(0, 0, 0, 1)">)
next()
})
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 全局路由后置守卫</span>
router.afterEach((to, from) =><span style="color: rgba(0, 0, 0, 1)"> {
console.log(</span>"全局路由后置守卫"<span style="color: rgba(0, 0, 0, 1)">)
})
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> router;</pre>
</div>
<p> </p>
<p> </p>
<p>main.js引入</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> main.js</span>
import Vue from 'vue'<span style="color: rgba(0, 0, 0, 1)">
import App from </span>'./App'<span style="color: rgba(0, 0, 0, 1)">
import router from </span>'./router'<span style="color: rgba(0, 0, 0, 1)">
import { RouterMount } from </span>'uni-simple-router'<span style="color: rgba(0, 0, 0, 1)">
App.mpType </span>= 'app'<span style="color: rgba(0, 0, 0, 1)">
const app </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Vue({
...App
})
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">v1.3.5起 H5端 你应该去除原有的app.$mount();使用路由自带的渲染方式</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> #ifdef H5</span>
RouterMount(app,'#app'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #endif</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #ifndef H5</span>
app.$mount(); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">为了兼容小程序及app端必须这样写才有效果</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> #endif</span></pre>
</div>
<p> </p>
<p>然后启动我们的项目进入页面就能看到进入路由了</p>
<p><img src="https://img2020.cnblogs.com/blog/1517521/202103/1517521-20210304162334109-936106143.png"></p>
<p> </p>
<p> <img src="https://img2020.cnblogs.com/blog/1517521/202103/1517521-20210304162348163-2117819584.png"></p>
<p> </p><br><br>
来源:https://www.cnblogs.com/yongwunaci/p/14481150.html
頁:
[1]