代码之家  ›  专栏  ›  技术社区  ›  Ahmer Ali Ahsan

角度5:我们如何使用RouteRoutlet将数据从嵌套组件传递到父组件?

  •  0
  • Ahmer Ali Ahsan  · 技术社区  · 6 年前

    我要做的是从登录组件发送数据->应用程序组件。

    import { Component, OnInit, EventEmitter, Output, Input } from '@angular/core';
    import { AuthService } from '../../services/auth.service';
    
    @Component({
      selector: 'app-login',
      templateUrl: './login.component.html',
      styleUrls: ['./login.component.css']
    })
    export class LoginComponent implements OnInit {
    
      isUserAuthenticated: boolean = false;
      @Output() valueChange = new EventEmitter();
    
      constructor(private auth: AuthService) { }
    
      ngOnInit() {
      }
    
      showMe() {
        this.isUserAuthenticated = this.isUserAuthenticated == false ? true : false;
        console.log('Log comming from login.component.ts: ' + this.isUserAuthenticated);
        this.auth.login(this.isUserAuthenticated);
        this.valueChange.emit(this.isUserAuthenticated);
      }
    }
    

    auth.service.ts代码:

    import { Injectable } from '@angular/core';
    import { Observable } from 'rxjs/Observable';
    
    @Injectable()
    export class AuthService {
      public isUserAuthenticated: boolean = false;
      constructor() { }
    
      login(params: boolean): Observable<boolean> {
        this.isUserAuthenticated = params;
        console.log('Log comming from auth.service.ts: ' + this.isUserAuthenticated);
        return new Observable<boolean>(observer => observer.next(this.isUserAuthenticated));
      }
    
    }
    

    import { Component } from '@angular/core';
    import { AuthService } from './services/auth.service';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      isUserAuthenticated: boolean = false;
      title = 'app';
    
      constructor(private auth: AuthService) {;
        console.log('Log comming from app.component.ts: ' + this.auth.isUserAuthenticated);
        this.isUserAuthenticated = this.auth.isUserAuthenticated;
      }
    
      showMe(value) {
        console.log('Receiving data from login.component.ts: ' + value);
        this.isUserAuthenticated = value;
      }
    }
    

    <div class='container-fluid'>
      <div class='row'>
        <div class='col-sm-3'>
          <app-nav-menu *ngIf="isUserAuthenticated"></app-nav-menu>
        </div>
        <div class='col-sm-9 body-content'>
          <router-outlet (valueChange)="showMe($event)"></router-outlet>
        </div>
      </div>
    </div>
    

    问题

    this.valueChange.emit(this.isUserAuthenticated); showMe(value) 在login.component.ts.中的函数。我不知道我的代码中做错了什么。请看一下,帮我一下。谢谢

    2 回复  |  直到 6 年前
        1
  •  1
  •   Lazar Ljubenović    6 年前

    tldr:使用一个服务并将其注入两个组件中。

    .您只能在模板中放置一个位置,路由器将在该位置动态实例化组件,因此输出是无用的,因为无法从模板访问它。

        2
  •  0
  •   Antoniossss    6 年前

    我要做的是从登录组件发送数据->应用程序组件。

    isAuthenticated

    然后你可以把它注入你的 login @Injectable S代表