angular2 - Pass value from parent route to child route(angular2 - 将值从父路由传递到子路由)
问题描述
我有一条叫做 home 的路由,它有三个子路由,documents,mail 和垃圾箱.在主路由组件中,它有一个名为用户"的变量.我知道有几种方法可以在父组件和子组件之间传递信息,突出显示 here,但我想如何在父/子路由之间传递信息.
I have a route called home and it has three child routes, documents, mail and trash. In the home route component it has a variable called 'user'. I know there are a few ways of passing info between parent and child components highlighted here, but how am I suppose to pass info between parent/child routes.
服务
组件
模板
推荐答案
您可以使用通用服务来传递数据,如 Angular 文档
You may use a common service to pass data like explained in the Angular Documentation
基本上,您可以创建一个具有用户对象的服务,一旦您的父路由被加载或对父组件进行一些操作,就可以更新该用户对象.
Basically you may create a Service which will have a user object, which can be updated once your parent route gets loaded or with some action on parent component.
用户服务
然后当子路由组件被加载时,您可以从服务中检索值.
And then when the child route component gets loaded you may retrieve the value from the Service.
主页组件
邮件组件
如果您在父级中添加提供者,则服务对象在子级中将是相同的实例.
Service object will be same instance in child if you add the provider in the parent.
这篇关于angular2 - 将值从父路由传递到子路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!