vue 2.0路由之路由嵌套示例詳解
前言
vue一個(gè)重要的方面就是路由,下面是自己寫的一個(gè)路由的例子分享給大家供大家參考學(xué)習(xí),下面來看看詳細(xì)的介紹。
方法如下:
1、引入依賴庫(kù)就不必再說
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í)例。記得要通過 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í)例。記得要通過 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í)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Vue實(shí)現(xiàn)數(shù)據(jù)篩選與搜索功能的示例代碼
在許多Web應(yīng)用程序中,數(shù)據(jù)篩選和搜索是關(guān)鍵的用戶體驗(yàn)功能,本文將深入探討在Vue中如何進(jìn)行數(shù)據(jù)篩選與搜索的實(shí)現(xiàn),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10
antd-DatePicker組件獲取時(shí)間值,及相關(guān)設(shè)置方式
這篇文章主要介紹了antd-DatePicker組件獲取時(shí)間值,及相關(guān)設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
基于Vue+elementUI實(shí)現(xiàn)動(dòng)態(tài)表單的校驗(yàn)功能(根據(jù)條件動(dòng)態(tài)切換校驗(yàn)格式)
這篇文章主要介紹了Vue+elementUI的動(dòng)態(tài)表單的校驗(yàn)(根據(jù)條件動(dòng)態(tài)切換校驗(yàn)格式),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
vue關(guān)于this.$refs.tabs.refreshs()刷新組件方式
這篇文章主要介紹了vue關(guān)于this.$refs.tabs.refreshs()刷新組件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Vue3中reactive與ref函數(shù)使用場(chǎng)景
這篇文章主要為大家介紹了Vue3?中有場(chǎng)景是?reactive?能做而?ref?做不了的使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07

