简体   繁体   English

路由器插座组件中的输入变量

[英]Input variable in a router-outlet component

I wanted to know if you guys now a way to have the @Input + ngOnChanges() combination or something doing the same inside a component in . 我想知道你们现在是否有办法让@Input + ngOnChanges()组合或在组件内部做同样的事情。

Basically, I have set a logged: boolean variable in my AppComponent and in my template I have : 基本上,我在我的AppComponent和我的模板中设置了一个logged: boolean变量:

<router-outlet></router-outlet>
<login [logged]="logged"></login>

What I want is to be able to watch that logged variable inside a component in the router-outlet so I make stuff here only when logged is as set as true. 我想要的是能够在路由器插座中的组件内观察 logged变量,所以我只在记录设置为true时才在这里制作东西。

I've tried to put <router-outlet [logged]="logged"></router-outlet> but that doesn't work, and using a variable in a service seems not to fit the ngOnChanges() watch. 我试图把<router-outlet [logged]="logged"></router-outlet>但是这不起作用,并且在服务中使用变量似乎不适合ngOnChanges()监视。

Has anyone an idea ? 有人有想法吗? Thanks ! 谢谢 !

Create a service that has an observable to subscribe to 创建具有可订阅的可观察服务

 import { Injectable } from '@angular/core'; import { Observable } from 'rxjs/Rx'; import { ReplaySubject } from 'rxjs/ReplaySubject'; @Injectable() export class UserSessionService { private _loggedIn: ReplaySubject<boolean> = new ReplaySubject<boolean>(); public logUserIn() { this._loggedIn.next( true ); } public logUserOut() { this._loggedIn.next( false ); } public getUserLoggedInObs(): Observable<boolean> { return this._loggedIn.asObservable(); } } 

Add the service to a parent module (be careful if adding it to multiple modules as you may get different instances of this service with different loggedIn values) 将服务添加到父模块(如果将其添加到多个模块,请小心,因为您可能使用不同的loggedIn值获得此服务的不同实例)

 import { NgModule } from '@angular/core'; import { UserSessionService } from 'wherever/service/is/located'; @NgModule({ providers: [ UserSessionService ] }) export class AppModule { } 

In your controller do something like 在你的控制器做类似的事情

 public userLoggedInObs: Observable<boolean>; constructor( private userSessionService: UserSessionService ) { this.userLoggedInObs = userSessionService.getUserLoggedInObs() } 

The in your View you can add 您可以在视图中添加

 <div *ngIf="userLoggedInObs | async"> User is logged in </div> <div *ngIf="!(userLoggedInObs | async)"> User isn't logged in </div> 

When you call the service it will broadcast to all components currently subscribed to it, which will then live update. 当您调用该服务时,它将广播到当前订阅它的所有组件,然后将进行实时更新。

Easiest way I would say is to use a service and add this service to your child 我想说的最简单的方法是使用服务并将此服务添加到您的孩子

@Injectable() 
export class LoginService {
    private logged:boolean;

    setlogged(data:boolean) {
        this.logged = data;
    }

    isLogged() {
        return this.logged;
    }

OR use localstorage 或使用localstorage

    set isLogged(value: boolean) {
        localStorage.setItem('logged', value);
    }

    get isLogged(): boolean {
        return <boolean> localStorage.getItem('logged'));
    }
}

In your component: 在您的组件中:

constructor(private loginService:LoginService) {}
logged: boolean;
ngOnInit() {
    this.logged = true;
    // variable:
    this.loginService.setLogged(logged);
    // Localstorage:
    localStorage.setItem('logged', logged);
}

In your child component: 在您的子组件中:

logged: boolean;
constructor(loginService:LoginService) {
    // variable:
    this.logged = this.loginService.isLogged();
    // Localstorage:
    this.logged = this.loginService.isLogged;
}

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

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