首页 > 解决方案 > Angular 6 - 传递变量值

问题描述

是否有任何选项可以仅将 Component1 中保存的一个变量的值传递给 Component2 中的变量而无需任何模板绑定?

我有组件页眉和页脚,在页眉中有一个布尔变量测试,我需要将变量 TEST 的值传递给页脚组件中的变量 TEST2。

我正在寻找一些带有@Input 的解决方案,但我没有找到任何没有模板绑定的解决方案,如 [test]="test"

简而言之,我只需要将值从一个.ts文件传递到另一个.ts文件。

我在关注这个例子:Pass data to nth level child component in Angular 4

但是变量仍然没有传递给组件

标头服务.ts

import { Injectable } from '@angular/core';

@Injectable()
export class HeaderService {
    getTheme: boolean;
}

页眉组件.ts

import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { SideBarService } from '../sidebar/sidebar.service';
import { googleMapConfig } from '../content/google-map/config.service';
import { HeaderService } from './header.service';

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.scss'],
  encapsulation: ViewEncapsulation.None,
})

export class HeaderComponent implements OnInit {

  Name = 'Menučkáreň';
  Navigation = 'Navigácia';
  nightTheme;

  icons = ['favorites','notification_important'];
  tooltip = ['Obľúbené položky','Zoznam zmien'];

  constructor(
    public sideBarService: SideBarService,
    public mapService: googleMapConfig,
    private headerService: HeaderService
  ) { }

public toggle() {
  this.sideBarService.toggle.emit();
}

public changeDark() {
  this.nightTheme = true;
  this.headerService.getTheme = true;
  this.mapService.changeDark.emit();
}

public changeLight() {
  this.nightTheme = false;
  this.headerService.getTheme = false;
  this.mapService.changeLight.emit();
}

  ngOnInit() { }

}

页脚组件

import { Component } from '@angular/core';
import { HeaderService } from '../header/header.service';

@Component({
  selector: 'app-footer',
  templateUrl: './footer.component.html',
  styleUrls: ['./footer.component.scss'],
  providers: [HeaderService]
})
export class FooterComponent {

  Copyright = 'Copyright 2018 | Vytvoril Patrik Spišák';
  Version = '0.0.1';
  nightTheme: boolean;

  constructor(private headerService: HeaderService) {

    this.nightTheme = this.headerService.getTheme;

   }

}

因此,当我从 HeaderComponent.html 调用我的函数changeDark()时,它不会触发 this.headerService.getTheme = true;

 <mat-grid-tile>
              <button (click)="this.changeDark($event)" mat-icon-button>
                <mat-icon aria-label="Nočný režim">brightness_3</mat-icon>
              </button>
          </mat-grid-tile>

更新

所以我能够用这段代码实现我所需要的:问题在于在 FooterComponent 中声明的提供程序。虽然在 FootersComponent 中声明了提供程序,但当我删除它们并仅在 app.modules.ts 变量中保留提供程序时,我得到了未定义的设置和正确读取。

标头服务.ts

import { Injectable } from '@angular/core';

@Injectable()
export class HeaderService {
    nightTheme:boolean;

    get data():boolean{
        return this.nightTheme;
    }

    set data(value:boolean){
        this.nightTheme = value;
    }

constructor(){}
}

标题组件

import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { SideBarService } from '../sidebar/sidebar.service';
import { googleMapConfig } from '../content/google-map/config.service';
import { HeaderService } from './header.service';

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.scss'],
  encapsulation: ViewEncapsulation.None,
})

export class HeaderComponent implements OnInit {

  Name = 'Menučkáreň';
  Navigation = 'Navigácia';

  icons = ['favorites','notification_important'];
  tooltip = ['Obľúbené položky','Zoznam zmien'];

  constructor(
    public sideBarService: SideBarService,
    public mapService: googleMapConfig,
    private headerService: HeaderService
  ) {}

public toggle() {
  this.sideBarService.toggle.emit();
}

public changeDark() {
  this.headerService.nightTheme = true;
  console.log(this.headerService.nightTheme);
  this.mapService.changeDark.emit();
}

public changeLight() {
  this.headerService.nightTheme = false;
  console.log(this.headerService.nightTheme);
  this.mapService.changeLight.emit();
}

  ngOnInit() { }

}

页脚组件

import { Component, OnInit } from '@angular/core';
import { HeaderService } from '../header/header.service';

@Component({
  selector: 'app-footer',
  templateUrl: './footer.component.html',
  styleUrls: ['./footer.component.scss'],
})
export class FooterComponent {

  Copyright = 'Copyright 2018 | Vytvoril Patrik Spišák';
  Version = '0.0.1';

  constructor(private headerService: HeaderService) { }

  ngOnInit() {
    console.log('FOOTER:'+this.headerService.nightTheme);
  }
  
}

标签: angulartypescript

解决方案


看看官方的Angular 文档——父母和孩子通过服务进行交流。这个想法是创建一个具有一些 RxJSSubject属性的服务。您的组件将注入此服务并使用这些Subjects - 它们可以相互交互。为什么Subject?因为您可以同时订阅和发出值。欲了解更多信息,请查看这篇关于理解主题的文章。

如果您不熟悉 RxJS 主题,作为一种解决方法(但这不是一个好的解决方案) - 您可以通过更改其属性来共享同一服务的值。但在这种情况下,如果有任何更改,您的组件将无法收到通知。这就是为什么主题是通过服务进行组件交互的良好候选者的原因。


推荐阅读