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.


激活阶段之后,在激活钩子解决后,在传入的组件上调用。当路由更改并且当前组件被重用时也会调用它。使用此钩子来加载和设置当前组件上的数据。

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).


调用transition.next(data)将在组件的数据中设置每个属性。例如,使用{a:1,b:2},路由器将调用组件$ 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.

激活钩子解析后立即调用数据转换挂钩,并在执行视图切换之前立即调用。进入组件获取一个** $ loadRouteData **元属性,它以值为true开始,并在数据钩子解析时设置为false。此属性可用于显示输入组件的加载状态。
解决时,组件还将发出“路由数据加载”事件。

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.

数据钩与激活不同:
即使当前组件被重用,每次路由更改也会调用数据,而激活只有当组件被新建时才被调用。
想象一下,我们有一个路由/ message /:id的组件,我们目前在/ message / 1。当用户导航到/ message / 2时,当前组件可以被重用,所以激活钩子不会被调用。但是,我们希望基于新的id参数获取和更新数据,因此在大多数情况下,使用数据获取数据而不是激活是有意义的。

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.

激活的责任是控制切换到新组件的时间。相比之下,数据在激活解决后立即被调用,恰好在视图切换发生之前,所以数据获取和新组件的输入动画将并行,并且组件将在数据解析之前处于“加载”状态。
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.
•如果我们等待在显示新组件之前获取数据,那么在视图切换之前,用户会觉得界面“卡住”了一秒钟。
• 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.
•相反,我们可以立即对用户输入做出反应,并开始切换视图,同时以“加载”状态显示新组件。如果我们之间有一个CSS转换,动画时间可以很好地与数据等待时间重叠。
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).
上述的并行数据获取示例要求我们利用Promise.all将多个Promises组合成一个,并且desturcturing和格式化仍然有点麻烦。 vue-router提供了一个语法糖,它允许您返回包含Promises的对象(当然也可以包含非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)
    }
  }
}

参考网址:https://github.com/vuejs/vue-router/blob/1.0/docs/en/pipeline/data.md

Contact GitHub API Training Shop Blog About

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,293评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,604评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,958评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,729评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,719评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,630评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,000评论 3 397
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,665评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,909评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,646评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,726评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,400评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,986评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,959评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,996评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,481评论 2 342

推荐阅读更多精彩内容

  • 我从小就知道自己唱歌唱得不好,因为很小的时候我爸就那样说了:小菊唱歌总是跟不上拍子,这样不好听呢呵呵! 我妈也附和...
    墻角的雏菊阅读 307评论 5 2
  • CentOS升级到7之后,发现无法使用iptables控制Linuxs的端口,google之后发现Centos 7...
    谭威_1509阅读 1,219评论 0 0
  • 时文 八年前2009年的情人节,读大学的女儿为了体验生活,心血来潮想去卖花,那年2月14日,学校还没开学,为...
    时间yi阅读 406评论 0 1