小编kev*_*rsc的帖子

Angular2 - 使用服务在组件之间共享数据

我有一个对象,我想在我的组件之间共享一个Angular2应用程序.

这是第一个组件的来源:

/* app.component.ts */

// ...imports
import {ConfigService} from './config.service';

@Component({
    selector: 'my-app',
    templateUrl: 'app/templates/app.html',
    directives: [Grid],
    providers: [ConfigService]
})
export class AppComponent {
    public size: number;
    public square: number;

    constructor(_configService: ConfigService) {
        this.size = 16;
        this.square = Math.sqrt(this.size);

        // Here I call the service to put my data
        _configService.setOption('size', this.size);
        _configService.setOption('square', this.square);
    }
}
Run Code Online (Sandbox Code Playgroud)

和第二部分:

/* grid.component.ts */

// ...imports
import {ConfigService} from './config.service';

@Component({
    selector: 'grid',
    templateUrl: 'app/templates/grid.html',
    providers: [ConfigService]
})
export class Grid {
    public config; …
Run Code Online (Sandbox Code Playgroud)

typescript angular2-services angular

27
推荐指数
2
解决办法
3万
查看次数

标签 统计

angular ×1

angular2-services ×1

typescript ×1