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

vue2.0嵌套路由實現(xiàn)豆瓣電影分頁功能(附demo)

 更新時間:2017年03月13日 10:59:21   作者:周星星學編程  
這篇文章主要介紹了vue2.0嵌套路由實現(xiàn)豆瓣電影分頁功能(附demo),這里整理了詳細的代碼,有需要的小伙伴可以參考下。

前言

最近練習Vue,看到官方文檔中的嵌套路由,不做不知道,實在是太坑了,網(wǎng)上資料demo少之又少,然后自己就做了一個demo,用了vue2.0嵌套路由實現(xiàn)豆瓣電影分頁功能,供大家學習學習,寫得不好望見諒。

demo截圖:


Demo簡單介紹

主路由:Top250(charts),正在熱映(hot),即將上映(ing),新片榜(newmovie)

const router = new VueRouter({
  routes: [
    {
     path: '/',  //設置默認路由為Top250
     component: charts
    },
    {
     path: '/charts', //Top250
     component: charts
    },
    {
     path: '/hot', 
     component: hot
    },
    {
     path: '/ing',
     component: ing
    },
    {
     path: '/newmovie',
     component: newmovie
    },
  ]
 }

在top250(charts)上添加了分頁功能作為子路由,在配置上添加:

{
 path: '/charts/:id', //子路由
 component: charts,
 children: [
  {path: '1', component: moviecontent}, 
  {path: '2', component: moviecontent2},
  {path: '3', component: moviecontent3}
  ]
}

在charts組件上添加入口:

<router-link to="/charts/1">1</router-link>
<router-link to="/charts/2">2</router-link>
<router-link to="/charts/3">3</router-link>

在charts組件上添加出口:

<router-view></router-view>

子路由如何跳轉同一組件時數(shù)據(jù)實現(xiàn)更新?

同樣,在top250(charts)上添加了分頁功能作為子路由,但指向同一組件:

{
 path: '/charts/:id', //子路由
 component: charts,
 children: [
  {path: '1', component: moviecontent2}, // 同一組件
  {path: '2', component: moviecontent2}, // 同一組件
  {path: '3', component: moviecontent2} // 同一組件
  ]
}

畫重點:

因為路由切換同一組件時,實例已經(jīng)在第一次進入路由時創(chuàng)建了,之后切換路由不會被創(chuàng)建了,所以只能調(diào)用一次created,因此要使用$route監(jiān)聽getData方法,當路由切換的時候,調(diào)用getData方法,重新獲取數(shù)據(jù)。

created: function () {
    //第一次進入路由時數(shù)據(jù)的更新
    this.$http.jsonp()
},
watch: {
 '$route': 'getData' //切換路由,調(diào)用getData方法
},
methods: {
 getData: function () {
    //路由切換,重新請求數(shù)據(jù)
    this.$http.jsonp()
 }
}

數(shù)據(jù)來自豆瓣電影API。

代碼寫得好爛,湊合著看吧,反正子路由還是成功的??!

本demo地址:doubandemo_jb51.rar

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

佛坪县| 鄂温| 文安县| 宜丰县| 六安市| 贺兰县| 湟中县| 泾源县| 镇坪县| 靖远县| 新巴尔虎左旗| 昌图县| 巴青县| 竹北市| 德兴市| 绥宁县| 宜良县| 富宁县| 荃湾区| 高邑县| 廊坊市| 襄垣县| 顺义区| 哈巴河县| 河间市| 呼伦贝尔市| 茶陵县| 大冶市| 万安县| 德化县| 巴彦淖尔市| 兰溪市| 始兴县| 临沭县| 宁陕县| 集贤县| 连山| 正镶白旗| 高平市| 巫山县| 泉州市|