最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue.js中Vue-router 2.0基礎(chǔ)實踐教程

 更新時間:2017年05月08日 09:52:32   作者:bboyjoe  
這篇文章主要給大家介紹了關(guān)于vue.js中Vue-router 2.0基礎(chǔ)實踐的相關(guān)資料,其中包括vue-router 2.0的基礎(chǔ)用法、動態(tài)路由匹配、嵌套路由、編程式路由、命名路由以及命名視圖等相關(guān)知識,需要的朋友可以參考借鑒,下面來一起看看吧。

前言

Vue.js的一大特色就是構(gòu)建單頁面應(yīng)用十分方便,既然要方便構(gòu)建單頁面應(yīng)用那么自然少不了路由,vue-router就是vue官方提供的一個路由框架。本文主要介紹了Vue-router 2.0的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面來看看詳細(xì)的介紹:

一、基礎(chǔ)用法:

<div id="app"> 
 <h1>Hello App!</h1> 
 <p> 
  <!-- 使用 router-link 組件來導(dǎo)航. --> 
  <!-- 通過傳入 `to` 屬性指定鏈接. --> 
  <!-- <router-link> 默認(rèn)會被渲染成一個 `<a>` 標(biāo)簽 --> 
  <router-link to="/foo">Go to Foo</router-link> 
  <router-link to="/bar">Go to Bar</router-link> 
 </p> 
 <!-- 路由出口 --> 
 <!-- 路由匹配到的組件將渲染在這里 --> 
 <router-view></router-view> 
</div> 
 
<template id='foo'> 
 <p>this is foo!</p> 
</template> 
<template id='bar'> 
 <p>this is bar!</p> 
</template> 
// 1. 定義(路由)組件。 
// 可以從其他文件 import 進(jìn)來 
const Foo = { template:'#foo' }; 
const Bar = { template:'#bar' }; 
// 2. 定義路由 
// 每個路由應(yīng)該映射一個組件。 其中"component" 可以是 
// 通過 Vue.extend() 創(chuàng)建的組件構(gòu)造器, 
// 或者,只是一個組件配置對象。 
const routes = [ 
 { path: '/foo', component: Foo }, 
 { path: '/bar', component: Bar } 
]; 
// 3. 創(chuàng)建 router 實例,然后傳 `routes` 配置 
// 你還可以傳別的配置參數(shù), 不過先這么簡單著吧。 
const router = new VueRouter({ routes:routes }); 
// 4. 創(chuàng)建和掛載根實例。 
// 記得要通過 router 配置參數(shù)注入路由, 
// 從而讓整個應(yīng)用都有路由功能 
const app = new Vue({ router:router }).$mount('#app'); 

二、動態(tài)路由匹配:

<div id="app"> 
 <h1>Hello App!</h1> 
 <p> 
  <router-link to="/user/foo/post/123">Go to Foo</router-link> 
  <router-link to="/user/bar/post/456">Go to Bar</router-link> 
 </p> 
 <router-view></router-view> 
</div> 
 
<template id='user'> 
 <p>User:{{ $route.params.id }},Post:{{$route.params.post_id}}</p> 
</template> 
// 1. 定義組件。 
const User = { 
 template:'#user', 
 watch:{ 
  '$route'(to,from){ 
   console.log('從'+from.params.id+'到'+to.params.id); 
  } 
 } 
}; 
// 2. 創(chuàng)建路由實例 (可設(shè)置多段路徑參數(shù)) 
const router = new VueRouter({ 
 routes:[ 
  { path:'/user/:id/post/:post_id',component:User } 
 ] 
}); 
//3. 創(chuàng)建和掛載根實例 
const app = new Vue({ router:router }).$mount('#app'); 

三、嵌套路由:

<div id="app"> 
 <h1>Hello App!</h1> 
 <p> 
  <router-link to="/user/foo">Go to Foo</router-link> 
  <router-link to="/user/foo/profile">Go to profile</router-link> 
  <router-link to="/user/foo/posts">Go to posts</router-link> 
 </p> 
 <router-view></router-view> 
</div> 
 
<template id='user'> 
 <div> 
  <h2>User:{{ $route.params.id }}</h2> 
  <router-view></router-view> 
 </div> 
</template> 
 
<template id="userHome"> 
 <p>主頁</p> 
</template> 
 
<template id="userProfile"> 
 <p>概況</p> 
</template> 
 
<template id="userPosts"> 
 <p>登錄信息</p> 
</template> 
// 1. 定義組件。 
const User = { 
 template:'#user' 
}; 
const UserHome = { 
 template:'#userHome' 
}; 
const UserProfile = { 
 template:'#userProfile' 
}; 
const UserPosts = { 
 template:'#userPosts' 
}; 
// 2. 創(chuàng)建路由實例 
const router = new VueRouter({ 
 routes:[ 
  { path:'/user/:id', component:User, 
   children:[ 
    // 當(dāng) /user/:id 匹配成功, 
    // UserHome 會被渲染在 User 的 <router-view> 中 
    { path: '', component: UserHome}, 
    // 當(dāng) /user/:id/profile 匹配成功, 
    // UserProfile 會被渲染在 User 的 <router-view> 中 
    { path:'profile', component:UserProfile }, 
    // 當(dāng) /user/:id/posts 匹配成功 
    // UserPosts 會被渲染在 User 的 <router-view> 中 
    { path: 'posts', component: UserPosts } 
   ] 
  } 
 ] 
}); 
//3. 創(chuàng)建和掛載根實例 
const app = new Vue({ router:router }).$mount('#app'); 

四、編程式路由:

<div id="app"> 
 <h1>Hello App!</h1> 
 <p> 
  <router-link to="/user/foo">Go to Foo</router-link> 
 </p> 
 <router-view></router-view> 
</div> 
 
<template id='user'> 
 <h2>User:{{ $route.params.id }}</h2> 
</template> 
 
<template id="register"> 
 <p>注冊</p> 
</template> 
// 1. 定義組件。 
const User = { 
 template:'#user' 
}; 
const Register = { 
 template:'#register' 
}; 
// 2. 創(chuàng)建路由實例 
const router = new VueRouter({ 
 routes:[ 
  { path:'/user/:id', component:User }, 
  { path:'/register', component:Register } 
 ] 
}); 
//3. 創(chuàng)建和掛載根實例 
const app = new Vue({ router:router }).$mount('#app'); 
 
//4.router.push(location) 
router.push({ path: 'register', query: { plan: 'private' }}); 

五、命名路由:

<div id="app"> 
 <h1>Named Routes</h1> 
 <p>Current route name: {{ $route.name }}</p> 
 <ul> 
  <li><router-link :to="{ name: 'home' }">home</router-link></li> 
  <li><router-link :to="{ name: 'foo' }">foo</router-link></li> 
  <li><router-link :to="{ name: 'bar', params: { id: 123 }}">bar</router-link></li> 
 </ul> 
 <router-view class="view"></router-view> 
</div> 
 
<template id='home'> 
 <div>This is Home</div> 
</template> 
 
<template id='foo'> 
 <div>This is Foo</div> 
</template> 
 
<template id='bar'> 
 <div>This is Bar {{ $route.params.id }}</div> 
</template> 
const Home = { template: '#home' }; 
const Foo = { template: '#foo' }; 
const Bar = { template: '#bar' }; 
 
const router = new VueRouter({ 
 routes: [ 
  { path: '/', name: 'home', component: Home }, 
  { path: '/foo', name: 'foo', component: Foo }, 
  { path: '/bar/:id', name: 'bar', component: Bar } 
 ] 
}); 
 
new Vue({ router:router }).$mount('#app'); 

六、命名視圖:

<div id="app"> 
 <router-link to="/">Go to Foo</router-link> 
 <router-view class="view one"></router-view> 
 <router-view class="view two" name="a"></router-view> 
 <router-view class="view three" name="b"></router-view> 
</div> 
 
<template id='foo'> 
 <div>This is Foo</div> 
</template> 
 
<template id='bar'> 
 <div>This is Bar {{ $route.params.id }}</div> 
</template> 
 
<template id='baz'> 
 <div>This is baz</div> 
</template> 
const Foo = { template: '#foo' }; 
const Bar = { template: '#bar' }; 
const Baz = { template: '#baz' }; 
 
const router = new VueRouter({ 
 routes: [ 
  { 
   path: '/', 
   components: { 
    default:Foo, 
    a:Bar, 
    b:Baz 
   } 
  } 
 ] 
}); 
 
new Vue({ router:router }).$mount('#app'); 

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • vue中引入高德地圖并多點標(biāo)注的實現(xiàn)步驟

    vue中引入高德地圖并多點標(biāo)注的實現(xiàn)步驟

    這篇文章主要介紹了vue中引入高德地圖并多點標(biāo)注,實現(xiàn)步驟是通過vue的方法引入地圖,初始化地圖,設(shè)置寬和高,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • VUE組件簡明講解

    VUE組件簡明講解

    組件是什么?組件是用基礎(chǔ)的元素組成的復(fù)雜的、可以重復(fù)使用的代碼單元,就相當(dāng)于疊疊樂一樣的快件,可以將這些復(fù)用的代碼封裝其起來構(gòu)成的組件可需要的時候進(jìn)行調(diào)用
    2022-08-08
  • 繪制flowable?流程圖的Vue?庫使用詳解

    繪制flowable?流程圖的Vue?庫使用詳解

    這篇文章主要為大家介紹了繪制flowable?流程圖的Vue?庫使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Vue狀態(tài)管理工具Vuex工作原理解析

    Vue狀態(tài)管理工具Vuex工作原理解析

    Vuex是一個專為Vue.js應(yīng)用程序開發(fā)的狀態(tài)管理模式,下面這篇文章主要給大家介紹了關(guān)于Vue中狀態(tài)管理器(vuex)詳解以及實際應(yīng)用場景的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • vue-cli構(gòu)建項目使用 less的方法

    vue-cli構(gòu)建項目使用 less的方法

    這篇文章主要介紹了vue-cli構(gòu)建項目使用 less,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • vue js秒轉(zhuǎn)天數(shù)小時分鐘秒的實例代碼

    vue js秒轉(zhuǎn)天數(shù)小時分鐘秒的實例代碼

    這篇文章主要介紹了vue js秒轉(zhuǎn)天數(shù)小時分鐘秒的實例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-08-08
  • 使用Vite2+Vue3渲染Markdown文檔的方法實踐

    使用Vite2+Vue3渲染Markdown文檔的方法實踐

    本文主要介紹了Vite2+Vue3渲染Markdown文檔的方法實踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的可以了解一下
    2021-08-08
  • vue-router 起步步驟詳解

    vue-router 起步步驟詳解

    這篇文章主要介紹了vue-router 起步步驟詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • 詳解Vue3的七種組件通信方式

    詳解Vue3的七種組件通信方式

    本篇文章將詳解介紹Vue3中如下七種組件通信方式:props、emit、v-model、refs、provide/inject、eventBus、vuex/pinia(狀態(tài)管理工具)。感興趣的可以了解一下
    2022-02-02
  • Vue頁面骨架屏的實現(xiàn)方法

    Vue頁面骨架屏的實現(xiàn)方法

    在開發(fā)webapp的時候總是會受到首屏加載時間過長的影響,越來越多的APP采用了“骨架屏”的方式去提升用戶體驗。這篇文章主要介紹了Vue頁面骨架屏的實現(xiàn)方法,感興趣的小伙伴們可以參考一下
    2018-05-05

最新評論

左权县| 青冈县| 开远市| 太和县| 聂荣县| 天津市| 肇源县| 南丰县| 花垣县| 山东省| 赤峰市| 枣强县| 永清县| 新丰县| 舞阳县| 三都| 赤城县| 瑞丽市| 随州市| 平定县| 清苑县| 沂水县| 孝昌县| 天津市| 杭州市| 阳山县| 隆德县| 英山县| 华亭县| 安新县| 诏安县| 昭苏县| 正定县| 栖霞市| 汝州市| 麻阳| 金华市| 仪陇县| 保定市| 屯留县| 晋州市|