【发布时间】:2023-03-07 22:01:01
【问题描述】:
是否有任何选项可以仅将 Component1 中保存的一个变量的值传递给 Component2 中的变量而不进行任何模板绑定?
我有组件 Header 和 Footer,并且在 header 中有一个布尔变量 test,我需要将变量 TEST 的值传递给 Footer 组件中的变量 TEST2。
我正在寻找一些带有 @Input 的解决方案,但我没有找到任何没有模板绑定的解决方案,如 [test]="test"
简而言之,我只需要将值从一个 .ts 文件传递到另一个 .ts 文件。
我在关注这个例子:Pass data to nth level child component in Angular 4
但是变量仍然没有传递给组件
HeaderService.ts
import { Injectable } from '@angular/core';
@Injectable()
export class HeaderService {
getTheme: boolean;
}
HeaderComponent.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 变量中保留提供程序时,已正确设置和读取。
HeaderService.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);
}
}
【问题讨论】:
-
您不能... 使用通用服务或模板语法。组件不应紧密耦合
-
你不能访问组件的属性,但是你可以访问你不想访问的组件中服务内部的值。
-
在您的源代码中,您没有在任何地方调用 changeDark 和 changeLight ,这就是您为服务设置值的地方。另外,我建议您通过编辑将源代码移至问题并删除答案。
-
对不起,我已经编辑了我的帖子。您可以看到实际上我调用了触发我定义的事件的函数,除了我需要传递给页脚组件的最后一个事件
-
@PatrikSpišák,在你的组件中使用 getter:不要声明 nightTheme,只需:get nightTheme(){ return this.headerService.getTheme;}
标签: angular typescript