摘要:生態(tài)之是什么是的路由系統(tǒng),定位資源的,我們可以不進(jìn)行整頁(yè)刷新去切換頁(yè)面內(nèi)容。
vue-router是Vue的路由系統(tǒng),定位資源的,我們可以不進(jìn)行整頁(yè)刷新去切換頁(yè)面內(nèi)容。
vue-router.js 可以下載 也可以用cdn,基本配置信息看如下代碼
// html 代碼 <div id="app"> <div> <router-link to="/">首頁(yè)router-link> <router-link to="/about">關(guān)于我們router-link> div> <div> <router-view>router-view> div> div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script> <script src="https://unpkg.com/vue-router/dist/vue-router.js">script> <script src="../js/router_demo.js">script> // js 代碼 var routes = [ { path: "/", component: { template: `<div><h1>首頁(yè)h1>div>` } }, { path: "/about", component: { template: `<div><h1>關(guān)于我們h1>div>` } } ] var router = new VueRouter({ routes: routes, // 路由去掉# // mode: history, }); var app = new Vue({ el: #app, router: router, });
// html 代碼 <div id="app"> <div> <router-link to="/">首頁(yè)router-link> <router-link to="/about">關(guān)于我們router-link> <router-link to="/user/琴女?age=20">琴女router-link> <router-link to="/user/提莫">提莫router-link> div> <div> <router-view>router-view> div> div> // js 代碼 var routes = [ { path: "/", component: { template: `<div><h1>首頁(yè)h1>div>` } }, { path: "/about", component: { template: `<div><h1>關(guān)于我們h1>div>` } }, { path: "/user/:name", component: { template: `<div> <h1>我是:{{$route.params.name}}h1> <h1>我年齡是:{{$route.query.age}}h1> div>`, } } ] var router = new VueRouter({ routes: routes, }); var app = new Vue({ el: #app, router: router, });
<div id="app"> <div> <router-link to="/">首頁(yè)router-link> <router-link :to="{name: about}">關(guān)于我們router-link> <router-link to="/user/gaoxin?age=19">gaoxinrouter-link> div> <div> <router-view>router-view> div> div> // js代碼 let routes = [ { path: /, component: { template: `<h1>這是主頁(yè)h1>` } }, { path: "/about", name: "about", component: { template: `<h1>關(guān)于我們h1>` } }, { path: "/user/:name", component: { template: `<div> <h1>我是{{$route.params.name}}h1> <h2>我的年齡是{{$route.query.age}}h2> div> ` } } ]; let router = new VueRouter({ routes: routes, mode: "history" }); const app = new Vue({ el: "#app", router: router, mounted(){ console.log(this.$route) console.log(this.$router) } })
// 添加子路由變化的只有父級(jí)路由 // 基于上面的例子增加 // js 代碼 { path: "/user/:name", component: { template: `<div> <h1>我是:{{$route.params.name}}h1> <h1>我年齡是:{{$route.query.age}}h1> <router-link to="more" append>更多信息router-link> <router-view>router-view> div>`, }, children: [ { path: "more", component: { template: `<div> {{$route.params.name}}的詳細(xì)信息 div>`, } } ] },
// 基于上面例子追加 // html 代碼 <div id="app"> <div> <router-link to="/">首頁(yè)router-link> <router-link to="/about">關(guān)于我們router-link> <router-link to="/user/琴女?age=20">琴女router-link> <router-link to="/user/提莫">提莫router-link> // 添加一個(gè)button按鈕 <button @click="on_click">旅游button> div> <div> <router-view>router-view> div> div> // js 代碼 // 注意路由name的使用 這是在原例子追加 var app = new Vue({ el: #app, router: router, methods: { on_click: function () { setTimeout(function () { this.$router.push(/about) setTimeout(function () { this.$router.push({name: "user", params:{name: "琴女"},query:{age: 20}}) }, 2000) }, 2000) } } });
命名路由視圖 router-view
當(dāng)我們只有一個(gè)
如果是content 和 footer 就需要同時(shí)顯示并且不同區(qū)域~這就需要對(duì)視圖進(jìn)行命名~
// html 代碼 <div id="app"> <div> <router-link to="/">首頁(yè)router-link> div> <div> <router-view name="content" class="content-view">router-view> <router-view name="footer" class="footer-view">router-view> div> div> // js 中的主要代碼 var routes = [ { path: "/", components: { content: { template: `<div><h1>首頁(yè)h1>div>`, }, footer: { template: `<div><h1>關(guān)于我們h1>div>`, } } }, ]
let routes = [
{
path: "**",
redirect: "/"
}
]
-- $route為當(dāng)前router調(diào)轉(zhuǎn)對(duì)象,里面可以獲取name, path, query, params等~
-- $router為VueRouter實(shí)例,有$router.push方法等~~
路由的生命周期就是從一個(gè)路由跳轉(zhuǎn)到另一路由整個(gè)過程,下面介紹兩個(gè)鉤子~
router.beforeEach() router.afterEach() 詳情請(qǐng)看代碼
// html 代碼 <div id="app"> <router-link to="/">首頁(yè)router-link> <router-link to="/login">登錄router-link> <router-link to="/user">用戶管理router-link> <div> <router-view>router-view> div> div> // js 代碼 var routes = [ { path: "/", component: { template: "<h1>首頁(yè)h1>" } }, { path: "/login", component: { template: "<h1>登錄h1>" } }, { path: "/user", component: { template: "<h1>用戶管理h1>" } } ]; var router = new VueRouter({ routes: routes }); router.beforeEach(function (to,from,next) { // console.log(to) // console.log(from) // console.log(next) // next(false) if(to.path=="/user"){ next("/login") } else { next(); } }); router.afterEach(function (to, from) { console.log(to) console.log(from) }); var app = new Vue({ el: #app, router: router });
next:function 一定要調(diào)用這個(gè)方法來resolve這個(gè)鉤子函數(shù)。
執(zhí)行效果依賴next方法的調(diào)用參數(shù)
next() 什么都不做繼續(xù)執(zhí)行到調(diào)轉(zhuǎn)的路由
next(false) 中斷當(dāng)前導(dǎo)航 沒有跳轉(zhuǎn) 也沒有反應(yīng)
next("/") 參數(shù)是路徑 調(diào)轉(zhuǎn)到該路徑
next(error) 如果next參數(shù)是一個(gè)Error實(shí)例 導(dǎo)航終止該錯(cuò)誤
會(huì)傳遞給router.onError()注冊(cè)過的回調(diào)中
如果/user下面還有子路由的情況下會(huì)怎么樣呢~????
// 匹配子路由 改一下匹配方法就可以~ // js 改動(dòng)代碼 router.beforeEach(function (to,from,next) { // console.log(to) // console.log(from) // console.log(next) // next(false) if(to.matched.some(function (item) { return item.path == "/post" })){ next("/login") } else { next(); } }); // 元數(shù)據(jù)配置 改動(dòng)代碼 // html 部分 { path: "/user", meta: { required_login: true, }, component: { template: ` <div> <h1>用戶管理h1> <router-link to="vip" append>viprouter-link> <router-view>router-view> div> ` }, children: [{ path: "vip", meta: { required_login: true, }, component: { template: <h1>VIPh1> } }] } // js 部分 router.beforeEach(function (to,from,next) { // console.log(to) // console.log(from) // console.log(next) // next(false) if(to.meta.required_login){ next("/login") } else { next(); } });
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/1530.html
摘要:前端技術(shù)棧還是非常龐大的,為了能夠借助已經(jīng)存在的輪子來造出一輛車,所以我選擇了進(jìn)行實(shí)踐。狀態(tài)的管理的狀態(tài)管理依靠完成,用其來管理的所有組件狀態(tài)。私有云客戶端打造主頁(yè)面首先是主頁(yè)面,可以打開任何一個(gè)云主機(jī)系統(tǒng)的頁(yè)面看,基本類似。 showImg(https://segmentfault.com/img/remote/1460000013930354); 【利用K8S技術(shù)棧打造個(gè)人私有...
摘要:前端技術(shù)棧還是非常龐大的,為了能夠借助已經(jīng)存在的輪子來造出一輛車,所以我選擇了進(jìn)行實(shí)踐。狀態(tài)的管理的狀態(tài)管理依靠完成,用其來管理的所有組件狀態(tài)。私有云客戶端打造主頁(yè)面首先是主頁(yè)面,可以打開任何一個(gè)云主機(jī)系統(tǒng)的頁(yè)面看,基本類似。 showImg(https://segmentfault.com/img/remote/1460000013930354); 【利用K8S技術(shù)棧打造個(gè)人私有...
摘要:導(dǎo)語(yǔ)承接上文實(shí)戰(zhàn)之后臺(tái)管理系統(tǒng)開發(fā)一在上一篇文章中,我詳細(xì)敘述了如何創(chuàng)建項(xiàng)目框架和引入各種后臺(tái)常用插件,做好這些準(zhǔn)備工作后,我們就可以著手進(jìn)行頁(yè)面的開發(fā)了。如果傳入的數(shù)據(jù)不符合規(guī)格,會(huì)發(fā)出警告。 1. 導(dǎo)語(yǔ) 承接上文:Vue 2.x 實(shí)戰(zhàn)之后臺(tái)管理系統(tǒng)開發(fā)(一) 在上一篇文章中,我詳細(xì)敘述了如何創(chuàng)建項(xiàng)目框架和引入各種后臺(tái)常用插件,做好這些準(zhǔn)備工作后,我們就可以著手進(jìn)行頁(yè)面的開發(fā)了。在開...
摘要:樣式通過標(biāo)簽包裹,默認(rèn)是影響全局的,如需定義作用域只在該組件下起作用,需在標(biāo)簽上加,如要引入外部文件,首先需給項(xiàng)目安裝依賴包,打開,進(jìn)入項(xiàng)目目錄,輸入回車。 showImg(https://segmentfault.com/img/remote/1460000013235090); (一)安裝node.js 首先需要安裝node環(huán)境,可以直接到中文官網(wǎng)http://nodejs.cn/...
閱讀 740·2023-04-25 19:43
閱讀 3986·2021-11-30 14:52
閱讀 3816·2021-11-30 14:52
閱讀 3873·2021-11-29 11:00
閱讀 3809·2021-11-29 11:00
閱讀 3907·2021-11-29 11:00
閱讀 3584·2021-11-29 11:00
閱讀 6197·2021-11-29 11:00