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);
    }
}

и второй компонент:

/* grid.component.ts */

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

@Component({
    selector: 'grid',
    templateUrl: 'app/templates/grid.html',
    providers: [ConfigService]
})
export class Grid {
    public config;
    public header = [];

    constructor(_configService: ConfigService) {
        // issue is here, the _configService.getConfig() get an empty object 
        // but I had filled it just before
        this.config = _configService.getConfig();
    }
  }

и наконец мой маленький сервис, ConfigService:

/* config.service.ts */

import {Injectable} from 'angular2/core';

@Injectable()
export class ConfigService {

    private config = {};

    setOption(option, value) {
        this.config[option] = value;
    }

    getConfig() {
        return this.config;
    }
}

Мои данные не передаются, в grid.component.ts,_configService.getConfig() строка возвращает пустой объект, но он заполняется непосредственно перед app.component.ts.

Я читал документы и учебники, ничего не получалось.

Чего мне не хватает?

Спасибо

РЕШИТЬ

Моя проблема заключалась в том, что я дважды вводил свой ConfigService. В начальной загрузке приложения и в файле, где я его использую.

Я удалилproviders сеттинг и его сработало!

Ответы на вопрос(0)

Ваш ответ на вопрос