最简单的方法是将此数组提取到
service
你可以注射到每一个
component
你想用。
@Injectable()
export class HeroesService {
heroes = [];
constructor(){
// you can request, or set your common array here
this.heroes = ........;
}
}
这样,您可以在启动服务时请求数组,并且每个组件都可以使用相同的服务。
@Component({
selector: ....
})
export class ExampleComponent{
constructor(private _hs: HeroesService){
// from this point, you can access the array as _hs.heroes;
}
}
(不要忘记将服务添加到
app.module
提供程序)查看如何精确地实现这一点,
check out the tutorial.