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

vue路由教程之靜態(tài)路由

 更新時間:2019年09月03日 14:49:44   作者:手指樂  
這篇文章主要給大家介紹了關(guān)于vue路由教程之靜態(tài)路由的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

前言

vue的單頁面應(yīng)用是基于路由和組件的,路由用于設(shè)定訪問路徑,并將路徑和組件映射起來。傳統(tǒng)的頁面應(yīng)用,是用一些超鏈接來實現(xiàn)頁面切換和跳轉(zhuǎn)的。在vue-router單頁面應(yīng)用中,則是路徑之間的切換,也就是組件的切換。

首先在html中,引入vue-router.js和vue.js,用router-link觸發(fā)路由跳轉(zhuǎn),router-link可以像a標簽一樣使用和定義樣式

router-view區(qū)域是路由匹配到的組件渲染的地方

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
 
<div id="app">
 <h1>Hello App!</h1>
 <p>
 <!-- 使用 router-link 組件來導(dǎo)航. -->
 <!-- 通過傳入 `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>

然后是js代碼

首先定義路由組件,組件可以是簡單的組件(template簡單定義即可),也可是extend定義的復(fù)雜組件

接下來定義路由映射表,就是定義路徑和組件之間的映射 

然后使用路由映射表創(chuàng)建路由對象 

最后使用路由對象創(chuàng)建vue對象,并掛載到指定元素

// 0. 如果使用模塊化機制編程,導(dǎo)入 Vue 和 VueRouter,要調(diào)用 Vue.use(VueRouter)
 
// 1. 定義(路由)組件。
// 可以從其他文件 import 進來
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }
 
// 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: routes
})
 
// 4. 創(chuàng)建和掛載根實例。
// 記得要通過 router 配置參數(shù)注入路由,
// 從而讓整個應(yīng)用都有路由功能
const app = new Vue({
 router// (縮寫)相當于 router: router
1
}).$mount('#app') // 現(xiàn)在,應(yīng)用已經(jīng)啟動了!

上例中,路由映射表實例名為routes,在創(chuàng)建路由對象時可以縮寫,如果不叫routes,比如叫routesa,則創(chuàng)建路由對象時必須寫routes:routesa

創(chuàng)建vue對象時,路由對象名也一樣,如果不叫router,也不能縮寫

使用extend創(chuàng)建模板

var todoItem = Vue.extend({
  data: function() {
   return {
    todoData: [
     { id: 0, text: '蔬菜' },
     { id: 1, text: '奶酪' },
     { id: 2, text: '隨便其它什么人吃的東西' }
    ]
   };
  },
  template: `
  <ul>
   <li v-for='(d, i) in todoData' :key="i">
    {{ d.text }}
   </li>
  </ul>
 `,
 
 });
 
 
 Home = { template: '<div>foo</div>' }
 About = { template: '<div>bar</div>' }
 
 routes = [
  { path: '/home', component: Home },
  { path: '/about', component: todoItem }
 ]
 
 router = new VueRouter({
  routes:routes // (縮寫)相當于 routes: routes
 });
 
 app = new Vue({
  router:router
 }).$mount('#app');

還可以這樣寫template

<!DOCTYPE html>
<!-- saved from url=(0077)https://keepfool.github.io/vue-tutorials/06.Router/basic/basic_01.html#!/home -->
<html>
 
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>abc</title>
 <link rel="stylesheet" >
 <link rel="stylesheet" href="./basic_01_files/custom.css" rel="external nofollow" >
</head>
 
<body>
 <div id="app">
  <div class="row">
   <div class="col-xs-offset-2 col-xs-8">
    <div class="page-header">
     <h2>Router Basic - 01</h2>
    </div>
   </div>
  </div>
  <div class="row">
   <div class="col-xs-2 col-xs-offset-2">
    <div class="list-group">
      
     <router-link class="list-group-item" to="/home">Go to Foo</router-link>
     <router-link class="list-group-item" to="/about">Go to Bar</router-link>
    </div>
   </div>
   <router-view></router-view>
  </div>
 </div>
 <template id="home">
  <div>
   <h1>Home</h1>
   <p>{{msg}}</p>
  </div>
 </template>
 
 <script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
 <script src="https://cdn.bootcss.com/vue-router/2.7.0/vue-router.min.js"></script>
 <script>
  
 var todoItem = Vue.extend({
  data: function() {
   return {
    todoData: [
     { id: 0, text: '蔬菜' },
     { id: 1, text: '奶酪' },
     { id: 2, text: '隨便其它什么人吃的東西' }
    ]
   };
  },
  template: `
  <ul>
   <li v-for='(d, i) in todoData' :key="i">
    {{ d.text }}
   </li>
  </ul>
 `,
 
 });
 
 var t_test = Vue.extend({
  data:function(){
   return {
    msg:"hello,test"
   };
  },
  template:"#home"
 
  }
 
 );
 
 
 
 // Home = { template: '<div>foo</div>' }
 // About = { template: '<div>bar</div>' }
 
 routes = [
  { path: '/home', component: t_test },
  { path: '/about', component: todoItem }
 ]
 
 router = new VueRouter({
  routes: routes // (縮寫)相當于 routes: routes
 });
 
 app = new Vue({
  router: router
 }).$mount('#app');
 </script>
</body>
 
</html>

如果不需要固定的導(dǎo)航鏈接,可以把router-link放在模板里面:

<!DOCTYPE html>
<html lang="en">
 
<head>
 <meta charset="UTF-8">
 <title>abc</title>
 <script src="https://unpkg.com/vue/dist/vue.js"></script>
 <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
 
<body>
 <div id="app">
  <h1>Hello App!</h1>
   
  <!-- 路由出口 -->
  <!-- 路由匹配到的組件將渲染在這里 -->
  <router-view>
   
  </router-view>
 </div>
</body>
<script type="text/javascript">
// 0. 如果使用模塊化機制編程,導(dǎo)入 Vue 和 VueRouter,要調(diào)用 Vue.use(VueRouter)
 
// 1. 定義(路由)組件。
// 可以從其他文件 import 進來
const Foo = { template: '<router-link to="/bar">Go to Bar</router-link>' }
const Bar = { template: '<router-link to="/foo">Go to Foo</router-link>' }
 
// 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: routes
})
 
// 4. 創(chuàng)建和掛載根實例。
// 記得要通過 router 配置參數(shù)注入路由,
// 從而讓整個應(yīng)用都有路由功能
const app = new Vue({
 router // (縮寫)相當于 router: router
 
}).$mount('#app') // 現(xiàn)在,應(yīng)用已經(jīng)啟動了!
</script>
 
</html>

進去的時候打網(wǎng)址

xxx/xx.html#/foo 或 xxx/xx.html#/bar

就可以實現(xiàn)foo和bar模板之間互相跳轉(zhuǎn)

也可以設(shè)置默認路由:

const routes = [
 { path: '/', component: Bar },
 { path: '/foo', component: Foo },
 { path: '/bar', component: Bar },
 
]

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。

相關(guān)文章

  • element-ui使用導(dǎo)航欄跳轉(zhuǎn)路由的用法詳解

    element-ui使用導(dǎo)航欄跳轉(zhuǎn)路由的用法詳解

    今天小編就為大家分享一篇element-ui使用導(dǎo)航欄跳轉(zhuǎn)路由的用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • vue引入組件的幾種方法代碼示例

    vue引入組件的幾種方法代碼示例

    vue的一個強大功能就是組件化開發(fā),下面這篇文章主要給大家介紹了關(guān)于vue引入組件的幾種方法,文中給出了詳細的代碼及圖文介紹,需要的朋友可以參考下
    2024-04-04
  • 如何在ElementUI的上傳組件el-upload中設(shè)置header

    如何在ElementUI的上傳組件el-upload中設(shè)置header

    這篇文章主要介紹了如何在ElementUI的上傳組件el-upload中設(shè)置header,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue實現(xiàn)手機端省市區(qū)區(qū)域選擇

    vue實現(xiàn)手機端省市區(qū)區(qū)域選擇

    這篇文章主要為大家詳細介紹了vue實現(xiàn)手機端省市區(qū)區(qū)域選擇,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • vue3.x中useRouter()執(zhí)行后返回值是undefined問題解決

    vue3.x中useRouter()執(zhí)行后返回值是undefined問題解決

    這篇文章主要給大家介紹了關(guān)于vue3.x中useRouter()執(zhí)行后返回值是undefined問題的解決方法,文中通過代碼示例介紹的非常詳細,對大家學(xué)習(xí)或者使用vue3.x具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-09-09
  • vue3組件庫Shake抖動組件搭建過程詳解

    vue3組件庫Shake抖動組件搭建過程詳解

    這篇文章主要為大家介紹了vue3組件庫Shake抖動組件搭建過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • 基于Vue框架vux組件庫實現(xiàn)上拉刷新功能

    基于Vue框架vux組件庫實現(xiàn)上拉刷新功能

    這篇文章主要為大家詳細介紹了基于Vue框架vux組件庫實現(xiàn)上拉刷新功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • vue之a(chǎn)-table中實現(xiàn)清空選中的數(shù)據(jù)

    vue之a(chǎn)-table中實現(xiàn)清空選中的數(shù)據(jù)

    今天小編就為大家分享一篇vue之a(chǎn)-table中實現(xiàn)清空選中的數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Vue 組件修改根實例的數(shù)據(jù)的方法

    Vue 組件修改根實例的數(shù)據(jù)的方法

    這篇文章主要介紹了Vue 組件修改根實例的數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • vue項目使用electron-builder庫打包成桌面程序的過程

    vue項目使用electron-builder庫打包成桌面程序的過程

    這篇文章主要介紹了vue項目使用electron-builder庫打包成桌面程序的過程,本文給大家介紹如何使用electron-builder這個庫結(jié)合實例代碼給大家講解的非常詳細,感興趣的朋友一起看看吧
    2024-02-02

最新評論

河南省| 贵德县| 绵竹市| 房山区| 布尔津县| 太和县| 沐川县| 拉孜县| 昌黎县| 三台县| 通化县| 博湖县| 黔江区| 鄯善县| 宝兴县| 武胜县| 朝阳市| 睢宁县| 南陵县| 佛冈县| 城市| 乌拉特中旗| 麻江县| 麻阳| 赤峰市| 乐陵市| 罗定市| 罗源县| 青川县| 涟源市| 大兴区| 清镇市| 海门市| 澄江县| 泗水县| 交口县| 长顺县| 汨罗市| 房产| 长岛县| 本溪市|