<tfoot id='Brct9'></tfoot>
    <bdo id='Brct9'></bdo><ul id='Brct9'></ul>
  • <small id='Brct9'></small><noframes id='Brct9'>

    <legend id='Brct9'><style id='Brct9'><dir id='Brct9'><q id='Brct9'></q></dir></style></legend>

        <i id='Brct9'><tr id='Brct9'><dt id='Brct9'><q id='Brct9'><span id='Brct9'><b id='Brct9'><form id='Brct9'><ins id='Brct9'></ins><ul id='Brct9'></ul><sub id='Brct9'></sub></form><legend id='Brct9'></legend><bdo id='Brct9'><pre id='Brct9'><center id='Brct9'></center></pre></bdo></b><th id='Brct9'></th></span></q></dt></tr></i><div id='Brct9'><tfoot id='Brct9'></tfoot><dl id='Brct9'><fieldset id='Brct9'></fieldset></dl></div>
      1. angular2 - 将值从父路由传递到子路由

        angular2 - Pass value from parent route to child route(angular2 - 将值从父路由传递到子路由)
        <i id='P5Jm6'><tr id='P5Jm6'><dt id='P5Jm6'><q id='P5Jm6'><span id='P5Jm6'><b id='P5Jm6'><form id='P5Jm6'><ins id='P5Jm6'></ins><ul id='P5Jm6'></ul><sub id='P5Jm6'></sub></form><legend id='P5Jm6'></legend><bdo id='P5Jm6'><pre id='P5Jm6'><center id='P5Jm6'></center></pre></bdo></b><th id='P5Jm6'></th></span></q></dt></tr></i><div id='P5Jm6'><tfoot id='P5Jm6'></tfoot><dl id='P5Jm6'><fieldset id='P5Jm6'></fieldset></dl></div>

        <small id='P5Jm6'></small><noframes id='P5Jm6'>

          <bdo id='P5Jm6'></bdo><ul id='P5Jm6'></ul>
            <tbody id='P5Jm6'></tbody>

              <legend id='P5Jm6'><style id='P5Jm6'><dir id='P5Jm6'><q id='P5Jm6'></q></dir></style></legend>
              1. <tfoot id='P5Jm6'></tfoot>
                1. 本文介绍了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.

                  { path: 'home',  component: HomeComponent, children: [
                          { path: 'documents',  component: DocumentsComponent },
                          { path: 'mail',  component: MailComponent },
                          { path: 'trash',  component: TrashComponent },
                      ]
                  },
                  

                  服务

                  import { Injectable } from '@angular/core';
                  @Injectable()
                  export class HomeService {
                    // Mock user, for testing  
                    myUser = {name:"John", loggedIn:true};
                    // Is Super Admin
                    isLogged():boolean {
                      if(this.myUser.role == true){
                        return true ; 
                      }
                      return false ; 
                    }
                  }
                  

                  组件

                    constructor(public router: Router, public http: Http, private homeService: HomeService) {
                  
                    }
                  
                    isLogged(){
                      return this.homeService.isLogged(); 
                    }
                  

                  模板

                  <div class="side-nav fixed" >
                      <li style="list-style: none">
                          <img alt="avatar" class="circle valign profile-image" height="64" src=
                          "../images/avatar.jpg" width="64">
                          <div class="right profile-name">
                              <!-- Value not changing even with service --> 
                              {{myUser.role}} 
                          </div>
                      </li>
                  

                  推荐答案

                  您可以使用通用服务来传递数据,如 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.

                  用户服务

                     import { Injectable } from '@angular/core';
                     import { Subject }    from 'rxjs/Subject';
                  
                     @Injectable()
                     export class UserService {
                       // Observable user 
                       user = new Subject<string>();
                     }
                  

                  然后当子路由组件被加载时,您可以从服务中检索值.

                  And then when the child route component gets loaded you may retrieve the value from the Service.

                  主页组件

                   @Component({
                     ... 
                   })
                   export class HomeComponent{
                     ... 
                     constructor(private userService:UserService ){}
                     someMethod = () =>{
                        this.userService.user.next(<pass user object>);
                     }
                   }
                  

                  邮件组件

                   @Component({
                     ... 
                   })
                   export class HomeComponent{
                     ... 
                     constructor(private userService:UserService ){
                       this.userService.user.subscribe(userChanged);  
                     }
                  
                     userChanged = (user) => {
                       // Do stuff with user
                     }
                   }
                  

                  如果您在父级中添加提供者,则服务对象在子级中将是相同的实例.

                  Service object will be same instance in child if you add the provider in the parent.

                  这篇关于angular2 - 将值从父路由传递到子路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))
                  <legend id='Yhl2L'><style id='Yhl2L'><dir id='Yhl2L'><q id='Yhl2L'></q></dir></style></legend>

                    <tbody id='Yhl2L'></tbody>
                  • <tfoot id='Yhl2L'></tfoot>
                    <i id='Yhl2L'><tr id='Yhl2L'><dt id='Yhl2L'><q id='Yhl2L'><span id='Yhl2L'><b id='Yhl2L'><form id='Yhl2L'><ins id='Yhl2L'></ins><ul id='Yhl2L'></ul><sub id='Yhl2L'></sub></form><legend id='Yhl2L'></legend><bdo id='Yhl2L'><pre id='Yhl2L'><center id='Yhl2L'></center></pre></bdo></b><th id='Yhl2L'></th></span></q></dt></tr></i><div id='Yhl2L'><tfoot id='Yhl2L'></tfoot><dl id='Yhl2L'><fieldset id='Yhl2L'></fieldset></dl></div>
                  • <small id='Yhl2L'></small><noframes id='Yhl2L'>

                    • <bdo id='Yhl2L'></bdo><ul id='Yhl2L'></ul>