简体   繁体   English

Angular 5 <router-outlet> 组件似乎在标头组件之前执行

[英]Angular 5 <router-outlet> components appears to execute before header component

I have a similar question to angular router-outlet load before header component, how to prevent? 我有一个类似的问题, 角度路由器 - 插头负载之前的标头组件,如何防止? ... I have a service locationService.ts, a headerComponent that import and inject the service and set values to an object defined in locationService...locationService.locationStatus. ...我有一个服务locationService.ts,一个headerComponent,用于导入和注入服务,并将值设置为locationService ... locationService.locationStatus中定义的对象。 I also have a homeComponent that import and inject locationService. 我还有一个导入和注入locationService的homeComponent。 However locationService is undefined when console.log it from the constructor on ngOnInit. 但是当console.log来自ngOnInit上的构造函数时,未定义locationService。 I need the examine the value set in the header before I display the html...I am using *ngIf to turn on/off a div in home.component.html depending on the value set in headerComponent. 在显示html之前,我需要检查标题中设置的值...我正在使用* ngIf打开/关闭home.component.html中的div,具体取决于headerComponent中设置的值。 My structure is like this: 我的结构是这样的:

app.compnent.html app.compnent.html

<custom-header></custom-header>

<router-outlet></router-outlet>

<custom-footer></custom-footer>

header.component.ts header.component.ts

....
...
import { LocationService } from '../locations/locations';
....
....
constructor(
   .....
    public locationService: LocationService,
   ......){
           this.setLocationStatus()
         }

.....
.....
setLocationStatus(){
       this.locationService.getStatus((data) => {
       .....
       ....
       this.locationService.locationStatus = data.status
       .....
       ...
     }

home.component.ts home.component.ts

.....
 import { LocationService } from '../locations/locations';
    ....
    ....

    activeLocation: boolean = false;
    constructor(
       .....
        public locationService: LocationService,
       ......){
         console.log(this.locationService.locationStatus)  <<<<< undefined
              }
     ngOinit(){
              this.activeLocation =
              this.locationService.locationStatus <<<<<undefined error
             }

It appears that the homeComponent is executed before the headerComponent?? 看来homeComponent是在headerComponent之前执行的? If this is so how can I achieve what i need to do which is the have the value set in the header and access it the homeComponent....Is there an alternate way to do this. 如果是这样的话我怎样才能实现我需要做的事情,即在标题中设置值并访问homeComponent ....是否有另一种方法可以做到这一点。

It is really very simple. 这真的很简单。 router-outlet are loaded with the components via the router and then the rest of the components are loaded as they are required by the current template (decided by the give route). router-outlet通过路由器加载组件,然后在当前模板需要时加载其余组件(由给定路由决定)。

if you need something to be decided before you reach to particular component (route) you should be using the route resolvers. 如果在到达特定组件(路线)之前需要确定某些事项,则应使用路线解析器。 and in that resolver you can inject the service you are trying to initiate in the header component. 在该解析器中,您可以在头部组件中注入要尝试启动的服务。 at the same time you can return the data / which you want to use as a deciding factor for *ngIf . 同时,您可以返回要用作*ngIf的决定因素的数据/。

Please read more about Angular Resolvers here 在此处阅读有关Angular Resolvers的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM