route data的用法

vue-router 1的用法
https://github.com/vuejs/vue-router/tree/1.0/docs/zh-cn
data(transition) [-> Promise]
Called on an incoming component during the activation phase, after the activate hook has been resolved. It is also called when the route has changed and the current component is reused. Use this hook to load and set data on the current component.


激活階段之后立哑,在激活鉤子解決后龟虎,在傳入的組件上調(diào)用仿野。當(dāng)路由更改并且當(dāng)前組件被重用時也會調(diào)用它节芥。使用此鉤子來加載和設(shè)置當(dāng)前組件上的數(shù)據(jù)篇恒。

Arguments
? transition {Transition}Calling transition.next(data) will set each property in data on the component. For example, with { a: 1, b: 2 }, the router will call component.$set('a', 1) and component.$set('b', 2).


調(diào)用transition.next(data)將在組件的數(shù)據(jù)中設(shè)置每個屬性核偿。例如敲董,使用{a:1空免,b:2}空另,路由器將調(diào)用組件$ set('a',1)和組件$ set('b'蹋砚,2)扼菠。

Expected Return Value
? optionally return a Promise.
? resolve(data) -> transition.next(data)
? reject(reason) -> transition.abort(reason)
** ? OR:** return an Object containing Promises. See Promise sugar below.


Details
The data transition hook is called immediately after the activate hook is resolved, and right before the view switching is executed. The entering component gets a $loadingRouteData meta property, which starts with value true and set to false when the data hook is resolved. This property can be used to display a loading state for the entering component.
When resolved, the component will also emit a 'route-data-loaded' event.

激活鉤子解析后立即調(diào)用數(shù)據(jù)轉(zhuǎn)換掛鉤摄杂,并在執(zhí)行視圖切換之前立即調(diào)用。進入組件獲取一個** $ loadRouteData **元屬性循榆,它以值為true開始析恢,并在數(shù)據(jù)鉤子解析時設(shè)置為false。此屬性可用于顯示輸入組件的加載狀態(tài)秧饮。
解決時映挂,組件還將發(fā)出“路由數(shù)據(jù)加載”事件。

The data hook is different from activate in that:

  1. data is also called every time the route changes, even if the current component is reused, while activate is only called when component is newly created.
    Imagine we have a component for the route /message/:id, and we are currently on /message/1. When the user navigates to /message/2, the current component can be reused, so the activate hook will not get called. But we do want to fetch and update the data based on the new id param, so in most cases it makes sense to do data fetching in data instead of activate.

數(shù)據(jù)鉤與激活不同:
即使當(dāng)前組件被重用盗尸,每次路由更改也會調(diào)用數(shù)據(jù)柑船,而激活只有當(dāng)組件被新建時才被調(diào)用。
想象一下泼各,我們有一個路由/ message /:id的組件鞍时,我們目前在/ message / 1。當(dāng)用戶導(dǎo)航到/ message / 2時历恐,當(dāng)前組件可以被重用寸癌,所以激活鉤子不會被調(diào)用。但是弱贼,我們希望基于新的id參數(shù)獲取和更新數(shù)據(jù)蒸苇,因此在大多數(shù)情況下,使用數(shù)據(jù)獲取數(shù)據(jù)而不是激活是有意義的吮旅。

2.activate's responsibility is controlling the timing of switching to the new component. In comparison, data is called right after activate is resolved and right before the view switching happens, so the data fetching and the new component's entering animation will go in parallel, and the component will be in a "loading" state before data is resolved.

激活的責(zé)任是控制切換到新組件的時間溪烤。相比之下,數(shù)據(jù)在激活解決后立即被調(diào)用庇勃,恰好在視圖切換發(fā)生之前檬嘀,所以數(shù)據(jù)獲取和新組件的輸入動畫將并行,并且組件將在數(shù)據(jù)解析之前處于“加載”狀態(tài)责嚷。
Let's consider the difference in the User Experience here:
? If we wait for the data to be fetched before displaying the new component, the user will feel like the interface is "stuck" for a split second before the view switches.
?如果我們等待在顯示新組件之前獲取數(shù)據(jù)鸳兽,那么在視圖切換之前,用戶會覺得界面“卡住”了一秒鐘罕拂。
? Instead, we can react to user input instantly and start switching the view, while displaying the new component with a "loading" state. If we have a CSS transition in between, the animation time can overlap nicely with the data wait time.
?相反揍异,我們可以立即對用戶輸入做出反應(yīng),并開始切換視圖爆班,同時以“加載”狀態(tài)顯示新組件衷掷。如果我們之間有一個CSS轉(zhuǎn)換,動畫時間可以很好地與數(shù)據(jù)等待時間重疊柿菩。
With that said, if you still wish to wait until data is loaded before switching the view, you can add waitForData: true inside your component's route option.


Examples
By calling transition.next:

route: {
  data: function (transition) {
    setTimeout(function () {
      transition.next({
        message: 'data fetched!'
      })
    }, 1000)
  }
}

By returning a Promise:

route: {
  data: function (transition) {
    return messageService
      .fetch(transition.to.params.messageId)
      .then(function (message) {
        return { message: message }
      })
  }
}

Parallel requests, with Promise & ES6:

route: {
  data ({ to: { params: { userId }}}) {
    return Promise.all([
      userService.get(userId),
      postsService.getForUser(userId)
    ]).then(([user, post]) => ({ user, post }))
  }
}

Equivalent of above in ES5:

route: {
  data (transition) {
    var userId = transition.to.params.userId
    return Promise.all([
      userService.get(userId),
      postsService.getForUser(userId)
    ]).then(function (data) {
      return {
        user: data[0],
        posts: data[1]
      }
    })
  }
}

Using $loadingRouteData in templates:

<div class="view">
  <div v-if="$loadingRouteData">Loading ...</div>
  <div v-if="!$loadingRouteData">
    <user-profile user="{{user}}"></user-profile>
    <user-post v-for="post in posts" :post="post"></user-post>
  </div>
</div>

Promise Sugar
The parallel data fetching example above requires us to leverage Promise.all to combine multiple Promises into a single one, and the desturcturing and formatting is still a bit cumbersome. vue-router provides a syntax sugar which allows you to return an Object containing Promises (it can contain non-Promise fields too, of course).
上述的并行數(shù)據(jù)獲取示例要求我們利用Promise.all將多個Promises組合成一個戚嗅,并且desturcturing和格式化仍然有點麻煩。 vue-router提供了一個語法糖,它允許您返回包含Promises的對象(當(dāng)然也可以包含非Promise字段)懦胞。
Here's the same example using the syntax sugar and ES6:

route: {
  data: ({ to: { params: { userId }}}) => ({
    user: userService.get(userId),
    post: postsService.getForUser(userId)
  })
}

The router will set the component's user and post fields to the corresponding Promises' resolved values when they are resolved. $loadingRouteData will be set to false when all Promises have been resolved.
Equivalent in ES5:

route: {
  data: function (transition) {
    var userId = transition.to.params.userId
    return {
      user: userService.get(userId),
      post: postsService.getForUser(userId)
    }
  }
}

參考網(wǎng)址:https://github.com/vuejs/vue-router/blob/1.0/docs/en/pipeline/data.md

Contact GitHub API Training Shop Blog About

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末替久,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子医瘫,更是在濱河造成了極大的恐慌侣肄,老刑警劉巖旧困,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件醇份,死亡現(xiàn)場離奇詭異,居然都是意外死亡吼具,警方通過查閱死者的電腦和手機僚纷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來拗盒,“玉大人怖竭,你說我怎么就攤上這事《赣” “怎么了痊臭?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長登夫。 經(jīng)常有香客問我广匙,道長,這世上最難降的妖魔是什么恼策? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任鸦致,我火速辦了婚禮,結(jié)果婚禮上涣楷,老公的妹妹穿的比我還像新娘分唾。我一直安慰自己,他們只是感情好狮斗,可當(dāng)我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布绽乔。 她就那樣靜靜地躺著,像睡著了一般碳褒。 火紅的嫁衣襯著肌膚如雪折砸。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天骤视,我揣著相機與錄音鞍爱,去河邊找鬼。 笑死专酗,一個胖子當(dāng)著我的面吹牛睹逃,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼沉填,長吁一口氣:“原來是場噩夢啊……” “哼疗隶!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起翼闹,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤斑鼻,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后猎荠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體坚弱,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年关摇,在試婚紗的時候發(fā)現(xiàn)自己被綠了荒叶。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡输虱,死狀恐怖些楣,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情宪睹,我是刑警寧澤愁茁,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站亭病,受9級特大地震影響鹅很,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜命贴,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一道宅、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧胸蛛,春花似錦污茵、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至民珍,卻和暖如春襟士,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背嚷量。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工陋桂, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蝶溶。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓嗜历,卻偏偏與公主長得像宣渗,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子梨州,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 我從小就知道自己唱歌唱得不好痕囱,因為很小的時候我爸就那樣說了:小菊唱歌總是跟不上拍子,這樣不好聽呢呵呵暴匠! 我媽也附和...
    墻角的雛菊閱讀 314評論 5 2
  • CentOS升級到7之后鞍恢,發(fā)現(xiàn)無法使用iptables控制Linuxs的端口,google之后發(fā)現(xiàn)Centos 7...
    譚威_1509閱讀 1,223評論 0 0
  • 時文 八年前2009年的情人節(jié)每窖,讀大學(xué)的女兒為了體驗生活帮掉,心血來潮想去賣花,那年2月14日岛请,學(xué)校還沒開學(xué)旭寿,為...
    時間yi閱讀 416評論 0 1