欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue.js中Vue-router 2.0基礎實踐教程

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

前言

Vue.js的一大特色就是構建單頁面應用十分方便,既然要方便構建單頁面應用那么自然少不了路由,vue-router就是vue官方提供的一個路由框架。本文主要介紹了Vue-router 2.0的相關內容,分享出來供大家參考學習,下面來看看詳細的介紹:

一、基礎用法:

<div id="app"> 
 <h1>Hello App!</h1> 
 <p> 
  <!-- 使用 router-link 組件來導航. --> 
  <!-- 通過傳入 `to` 屬性指定鏈接. --> 
  <!-- <router-link> 默認會被渲染成一個 `<a>` 標簽 --> 
  <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 進來 
const Foo = { template:'#foo' }; 
const Bar = { template:'#bar' }; 
// 2. 定義路由 
// 每個路由應該映射一個組件。 其中"component" 可以是 
// 通過 Vue.extend() 創(chuàng)建的組件構造器, 
// 或者,只是一個組件配置對象。 
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ù)注入路由, 
// 從而讓整個應用都有路由功能 
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ù)) 
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:[ 
    // 當 /user/:id 匹配成功, 
    // UserHome 會被渲染在 User 的 <router-view> 中 
    { path: '', component: UserHome}, 
    // 當 /user/:id/profile 匹配成功, 
    // UserProfile 會被渲染在 User 的 <router-view> 中 
    { path:'profile', component:UserProfile }, 
    // 當 /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'); 

總結

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

相關文章

  • vue實現(xiàn)驗證碼輸入框組件

    vue實現(xiàn)驗證碼輸入框組件

    最近做項目遇到這樣的需求要求輸入4位或6位短信驗證碼,輸入完成后收起鍵盤。實現(xiàn)步驟大家參考下本文
    2017-12-12
  • Vue組件生命周期運行原理解析

    Vue組件生命周期運行原理解析

    這篇文章主要介紹了Vue組件生命周期運行原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-11-11
  • VUE前端導出文件之file-saver插件安裝使用教程

    VUE前端導出文件之file-saver插件安裝使用教程

    這篇文章主要給大家介紹了關于VUE前端導出文件之file-saver插件安裝使用的相關資料,file-saver是一個用于保存文件的JavaScript庫,它提供了一種簡單的方式來生成和保存文件,支持各種文件類型,例如文本文件、圖片、PDF等,需要的朋友可以參考下
    2024-05-05
  • vue2.0使用swiper組件實現(xiàn)輪播效果

    vue2.0使用swiper組件實現(xiàn)輪播效果

    這篇文章主要為大家詳細介紹了vue2.0使用swiper組件實現(xiàn)輪播效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • vue中Vue.set()的使用以及對其進行深入解析

    vue中Vue.set()的使用以及對其進行深入解析

    vue不允許在已經創(chuàng)建的實例上動態(tài)添加新的根級響應式屬性,不過可以使用Vue.set()方法將響應式屬性添加到嵌套的對象上,下面這篇文章主要給大家介紹了關于vue中Vue.set()的使用以及對其進行深入解析的相關資料,需要的朋友可以參考下
    2023-01-01
  • 詳解webpack打包vue項目之后生成的dist文件該怎么啟動運行

    詳解webpack打包vue項目之后生成的dist文件該怎么啟動運行

    這篇文章主要介紹了詳解webpack打包vue項目之后生成的dist文件該怎么啟動運行,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • 如何在vue3+ts項目中使用query和params傳參

    如何在vue3+ts項目中使用query和params傳參

    Vue3中的路由傳參有兩種方式:query和params,下面這篇文章主要給大家介紹了關于如何在vue3+ts項目中使用query和params傳參的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-04-04
  • vue中的數(shù)字滾動和翻牌器

    vue中的數(shù)字滾動和翻牌器

    這篇文章主要介紹了vue中的數(shù)字滾動和翻牌器,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Vue2?特殊符號讓人傻傻分不清?“:”、“.”、“@”、“#”?、“{{}}“?、“$“,‘$bus‘,‘$event‘

    Vue2?特殊符號讓人傻傻分不清?“:”、“.”、“@”、“#”?、“{{}}“?、“$“,‘$bus‘,‘$e

    :”是指令?“v-bind”的縮寫“.”是修飾符?“@”是指令“v-on”的縮寫?,它用于監(jiān)聽?DOM?事件?“#”是v-slot的縮寫,這篇文章主要介紹了Vue2?新手上路無處不在的特殊符號讓人傻傻分不清“:”、“.”、“@”、“#”?、“{{}}“?、“$“,$bus,$event,需要的朋友可以參考下
    2024-08-08
  • vue resource發(fā)送請求的幾種方式

    vue resource發(fā)送請求的幾種方式

    這篇文章主要介紹了vue resource發(fā)送請求的幾種方式,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09

最新評論