vue 2.0路由之路由嵌套示例詳解
前言
vue一個(gè)重要的方面就是路由,下面是自己寫的一個(gè)路由的例子分享給大家供大家參考學(xué)習(xí),下面來(lái)看看詳細(xì)的介紹。
方法如下:
1、引入依賴庫(kù)就不必再說(shuō)
2、創(chuàng)建組件
兩種寫法
第一種:間接
<template id="home"> <div> <h1>Home</h1> <p>{{msg}}</p> </div> </template>
var About = Vue.extend({ template: '#about' });
第二種:直接
var Out = Vue.extend({ template: '<div><h1>Out</h1><p>This is the tutorial out vue-router.</p></div>' });
3、創(chuàng)建 router 實(shí)例,傳 'routes'路由映射配置
var router = new VueRouter({ routes: [ { path: '/路徑', component: 組件名 }, { path: '/', component: 組件名}, //設(shè)置默認(rèn)路徑 { path: "*", component:Home }//路徑不存在 <br> ] });
4、創(chuàng)建和掛載根實(shí)例。記得要通過(guò) router 配置參數(shù)注入路由,從而讓整個(gè)應(yīng)用都有路由功能
var vm = new Vue({ router: router }).$mount('#app');
整體的demo
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>hello world</title> </head> <body> <div id="app"> <div> <!-- 4、<router-link>默認(rèn)會(huì)被渲染成一個(gè) `<a>` 標(biāo)簽 ,to指令跳轉(zhuǎn)到指定路徑 --> <router-link to="/home">Go to Home</router-link> <router-link to="/about">Go to About</router-link> <router-link to="/out">Go to Out</router-link> </div> <!-- 5、在頁(yè)面上使用<router-view></router-view>標(biāo)簽,用于渲染匹配的組件 --> <!--這里顯示的是展示的界面--> <router-view></router-view> </div> <template id="home"> <div> <h1>Home</h1> <p>{{msg}}</p> </div> </template> <template id="about"> <div> <h1>about</h1> <p>This is the tutorial about vue-router.</p> </div> </template> <!-- 0、引入依賴庫(kù) --> <script src="../js/vue2.0.js" type="text/javascript" charset="utf-8"></script> <script src="lib/vue-router.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> /* 1、創(chuàng)建組件 */ var Home = Vue.extend({ template: '#home', data: function() { return { msg: 'Hello, vue router!' } } }); var About = Vue.extend({ template: '#about' }); var Out = Vue.extend({ template: '<div><h1>Out</h1><p>This is the tutorial out vue-router.</p></div>' }); // 2. 創(chuàng)建 router 實(shí)例,然后傳 `routes`路由映射 配置 var router = new VueRouter({ routes: [ { path: '/home', component: Home }, { path: '/about', component: About }, { path: '/out', component: Out }, {path: '/', component: Home },//設(shè)置默認(rèn)路徑 { path: "*", component:Home }//路徑不存在 ] }); // 3. 創(chuàng)建和掛載根實(shí)例。記得要通過(guò) router 配置參數(shù)注入路由,從而讓整個(gè)應(yīng)用都有路由功能 var vm = new Vue({ router: router }).$mount('#app'); // 現(xiàn)在,應(yīng)用已經(jīng)啟動(dòng)了! </script> </body> </html>
關(guān)于路由嵌套
在配置routes映射時(shí)添加children配置
如下:
var router = new VueRouter({ routes:[ {path:'/home',component:Home, children:[//子路由 {path:'news',component:News}, {path:'change',component:change} ]}, {path:'/me',component:Me}, {path:'/',component:Me} ] })
關(guān)于具體的demo可以參考GitHub上,另外還總結(jié)了一些自己最近在學(xué)習(xí)的阿里云上傳圖片等,會(huì)逐步更新,敬請(qǐng)指教!
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
通過(guò)vue-router懶加載解決首次加載時(shí)資源過(guò)多導(dǎo)致的速度緩慢問(wèn)題
這篇文章主要介紹了vue-router懶加載解決首次加載時(shí)資源過(guò)多導(dǎo)致的速度緩慢問(wèn)題,文中單獨(dú)給大家介紹了vue router路由懶加載問(wèn)題,需要的朋友可以參考下2018-04-04Vue-Cli配置代理轉(zhuǎn)發(fā)解決跨域問(wèn)題的方法
本文主要介紹了Vue-Cli配置代理轉(zhuǎn)發(fā)解決跨域問(wèn)題的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06導(dǎo)致VUE頁(yè)面不刷新的問(wèn)題分析及解決方法
由于 Vue 會(huì)在初始化實(shí)例時(shí)對(duì) property 執(zhí)行 getter/setter 轉(zhuǎn)化,所以 property 必須在 data 對(duì)象上存在才能讓 Vue 將它轉(zhuǎn)換為響應(yīng)式的,這篇文章主要介紹了導(dǎo)致VUE頁(yè)面不刷新的問(wèn)題分析及解決方法,需要的朋友可以參考下2024-04-04詳解vuex中mapState,mapGetters,mapMutations,mapActions的作用
這篇文章主要介紹了vuex中mapState,mapGetters,mapMutations,mapActions的作用,需要的朋友可以參考下2018-04-04vue3-vue-router創(chuàng)建靜態(tài)路由和動(dòng)態(tài)路由方式
這篇文章主要介紹了vue3-vue-router創(chuàng)建靜態(tài)路由和動(dòng)態(tài)路由方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10Vue?transition組件簡(jiǎn)單實(shí)現(xiàn)數(shù)字滾動(dòng)
這篇文章主要為大家介紹了Vue?transition組件簡(jiǎn)單實(shí)現(xiàn)數(shù)字滾動(dòng)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09