你好我基本上是Angular的新手,在这里我试图从服务中访问一个布尔变量。我已经在服务中声明了一个值为 true 的布尔变量。基于一些逻辑,我将把该服务变量更改为 false。我在几个组件中使用的那个特定变量。所以我面临这样的问题,我分配的初始值是真实的,它会出现在我使用过的所有组件中,但是在更改了未反映在所有组件中的值之后。像这样的服务类:
@Injectable()
export class MyService {
running: boolean = true;
}
组件 1:
export class FirstComponent {
constructor(private myService: MyService) { }
check(){
this.myservice.running = false;
}
}
组件 2:
export class SecondComponent implements OnChanges {
running;
constructor(private myService: MyService) { }
ngOnChanges(){
this.running = this.myService.running;
console.log('running', this.running);
}
}
这就是我将如何更改布尔值并从不同组件中的服务访问。我不确定这是否是正确的方法,请纠正我。