Я изучаю Angular2, создавая пример, в котором я хочу, чтобы щелкнул кнопкой мыши по Component1, который выполняет вызов ajax к Сервису, а ответ ajax должен использоваться и отображаться в другом компоненте.

Я могу создать Component1 и получить ответ, сделав вызов ajax в классе Service.Теперь, как я могу отобразить результат в другом компоненте
Это мой первый компонент:
import { Component } from '@angular/core';
import { ProfileService } from '../shared/index';
@Component({
selector: 'home-page',
template: `
<div>
<button (click)="loadUser()">Load profile</button>
{{ profile | json }}
</div>
`
})
export class ProfileComponent {
constructor(private profileService: ProfileService) {}
profile = {};
loadUser() {
this.profileService.getUser().subscribe(data => this.profile = data);
}
}
Это мой класс обслуживания:
import { Injectable } from '@angular/core';
import { HttpClient, Response } from '@angular/common/http';
import 'rxjs/add/operator/map';
@Injectable()
export class ProfileService {
constructor (
private http: HttpClient
) {}
getUser() {
return this.http.get(`https://conduit.productionready.io/api/profiles/eric`)
.map(res => res );
}
}
Это мой второйкомпонент, в котором я хочу увидеть результат:
import { Component } from '@angular/core';
@Component({
selector: 'result-page',
template: `
<div>Result Page :
{{ profile | json }}
</div>
`
})
export class ResultComponent {
constructor(private profileService: ProfileService) {}
profile = {};
username = "";
bio = "";
}
По сути, ответ ajax - это содержимое Json, я хочу сохранить весь файл JSON в файле profile
.Этот json содержит поля для имени пользователя и биографии, я хочу сохранить их в своих переменных имени пользователя и био моего компонента результата.
Я застрял, как создать свой компонент результата, не могли бы вы мне помочь.
Я хочу общаться между компонентами, не хочу использовать здесь какие-либо маршрутизаторы.
Ответ json:
{
"profile": {
"username": "eric",
"bio": "Cofounder of Thinkster.io, kinda looks like Peeta from the Hunger Games",
"image": "http://i.imgur.com/S66L2XZ.jpg",
"following": false
}
}