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

vue學(xué)習(xí)之Vue-Router用法實例分析

 更新時間:2020年01月06日 10:00:50   作者:theVicTory  
這篇文章主要介紹了vue學(xué)習(xí)之Vue-Router用法,結(jié)合實例形式分析了Vue-Router路由原理與常見操作技巧,需要的朋友可以參考下

本文實例講述了vue學(xué)習(xí)之Vue-Router用法。分享給大家供大家參考,具體如下:

Vue-router就像一個路由器,將組件(components)映射到路由(routes)后,通過點擊<router-link>它可以在<router-view>中將相應(yīng)的組件渲染出來。

1、使用vue-router的步驟

  //1、創(chuàng)建路由組件
  const Link1={template:'#link1'};
  const Link2={template:'#link2'};
  const Link3={template:'#link3'};
  //2、定義路由映射
  const routes=[
    {
      path:'/link1',       //定義相對路徑
      component:Link1,      //定義頁面的組件
      children:[
        {
          path:'intro',      //子路由/link1/intro
          component:{template:'#ariesIntro'},
          name:'ariesIntro',    //為路由命名
        },
        {
          path:'feature',
          component:{template:'#ariesFeature'},
        },
        {path:'/',redirect:'intro'}
      ]
    },
    {path:'/link2',component:Link2},
    {path:'/link3',component:Link3},
    {path:'/',redirect:'/link1'}        //配置根路由,使其重定向到/link1
  ];
  //3、創(chuàng)建router實例
  const router = new VueRouter({
    routes                   //縮寫,相當于 routes: routes
  });
  // 4、 創(chuàng)建和掛載根實例。
  const app = new Vue({
    router
  }).$mount('#app');              //掛載到id為app的div

注意:要設(shè)置根路由,頁面加載完成后默認顯示根路由,否則將什么也不顯示。

在頁面中調(diào)用路由接口,<router-link> 默認會被渲染成一個 `<a>` 標簽,點擊后在<router-view>渲染指定模板

  <div class="col-lg-offset-2 col-lg-2">
    <!-- 使用 router-link 組件來導(dǎo)航. -->
    <!-- 通過傳入 `to` 屬性指定鏈接. -->
    <router-link class="list-group-item" :to="{name:'ariesIntro'}">白羊座</router-link>
    <router-link class="list-group-item" to="/link2">處女座</router-link>
    <router-link class="list-group-item" to="/link3">金牛座</router-link>
  </div>
  <div class="col-lg-6">
    <div class="panel">
      <div class="panel-body">
        <!-- 路由出口,路由匹配到的組件將渲染在這里 -->
        <router-view></router-view>
      </div>
    </div>
  </div>

2、嵌套路由

通過每個路由內(nèi)的children數(shù)組屬性可以為每個路由再配置子路由,子路由的路徑是基于父路由目錄下的,默認路徑會進行疊加。例如上面再link1中添加intro與feature兩個子路由,在link1模板中使用兩個子路由:

  <template id="link1">
    <div>
      <h3>白羊座</h3>
      <ul class="nav nav-tabs">
        <li class="active">
          <router-link to="/link1/intro">簡介</router-link>
        </li>
        <li><router-link to="/link1/feature">特點</router-link></li>
      </ul>
      <div class="tab-content">
        <router-view></router-view>
      </div>
    </div>
  </template>

最終效果如圖:

3、動態(tài)路由

在路由路徑中綁定變量,使其根據(jù)不同的變量值跳轉(zhuǎn)到不同頁面,例如將path:"goods/:goodsId",假如當goodsId為15時,就會路由到/goods/15頁面。

4、編程路由

通過js代碼控制路由頁面的跳轉(zhuǎn)與傳值。例如給button綁定事件jump,在methods內(nèi)實現(xiàn):

jump(){
  this.$router.push('/cart?goodsId=123')
}

頁面跳轉(zhuǎn)到根下的cart目錄,并且傳遞參數(shù)goodsId,值為123。在cart頁面通過$route.query接收參數(shù),直接在頁面內(nèi)使用:

注意:區(qū)分跳轉(zhuǎn)是$router,接收參數(shù)是$route

<span>商品ID:{{$route.query.goodsId}}</span>

也可以指定頁面向前向后跳轉(zhuǎn):this.$router.go(2),向前跳轉(zhuǎn)兩步 ,向后跳轉(zhuǎn)一步go(-1)。

5、命名路由

當路由路徑過長時,也可以用name屬性為路徑命名,在<router-link>中使用動態(tài)綁定:to="{name:'路徑名'}"訪問。例如白羊座的鏈接上使用 :to="{name:'ariesIntro'}",可直接跳轉(zhuǎn)到link1下的Intro頁面。

還可以對視圖進行命名,將組件渲染到指定視圖位置,例如在父組件中有一個默認視圖與兩個命名視圖一左一右:  

<router-view></router-view>
<router-view class="left" name="cartview"></router-view>
<router-view class="right" name="imgview"></router-view>

在根路由中設(shè)置其組件components,將默認視圖渲染為GoodsList,左邊cartview視圖渲染為Cart組件,右邊imgview渲染為Image組件:

new Router({
 routes: [
  {
   path: '/',
   components:{
    default:GoodsList,
    cartview:Cart,
    imgview:Image
   }
}

結(jié)果如下:

希望本文所述對大家vue.js程序設(shè)計有所幫助。

相關(guān)文章

最新評論

云和县| 习水县| 石城县| 彭泽县| 三河市| 德钦县| 珠海市| 峨眉山市| 江安县| 桦川县| 江孜县| 简阳市| 云林县| 五指山市| 宁波市| 岑溪市| 怀仁县| 游戏| 巴塘县| 德庆县| 中宁县| 葵青区| 宜良县| 合阳县| 大竹县| 庄河市| 蓬溪县| 丽江市| 兴国县| 梓潼县| 靖西县| 杨浦区| 营口市| 陵水| 沙洋县| 栖霞市| 图木舒克市| 广西| 锦屏县| 新竹市| 汤原县|