我有这个简单的服务,这是一个两provider
模块:Angular 5:组件之间共享数据的服务(跨模块)?
@Injectable()
export class PlatformPickerService {
platforms: string[] = [
'macOS', 'Linux', 'Windows'
];
public platform: string;
constructor() {
this.platform = this.platforms[1];
}
public getPlatform(): string {
const platform = localStorage.getItem('platform');
return platform == null ? this.platform : platform;
}
public setPlatform(platform: string) {
this.platform = platform;
localStorage.setItem('platform', platform);
}
}
不幸的是,无处我用它收购了选择的值。在Module之外使用它只给出默认值,在Module中使用它不会给出任何值。
全测试用例:https://stackblitz.com/edit/angular-aru94g-2djexv
更改服务中的默认值是否在下拉框中生效? – nayakam
是的,它的确如此。 –
是吗?在你的演示中,默认显示MacOS,但在PlatformPickerService默认值设置为“Linux”的平台[1]。 – nayakam