我有一個簡單的組件,從服務器檢索數據時,如何從REST服務的數據。使用angular2服務器渲染
@Component({
selector: 'app',
template: `
<h1>HEllo {{category}}</h1>
`,
providers: [
HTTP_PROVIDERS,
CategoryService
]
})
export class App implements OnInit {
private category;
constructor(private _categoryService: CategoryService){}
ngOnInit() {
this._categoryService.getCategories().subscribe(categories => this.category = categories.name);
}
}
我的服務看起來像
@Injectable()
export default class CategoryService {
constructor(private http: Http){}
private _categoriesUrl = 'http://localhost:3000/api/photo';
getCategories() {
return this.http.get(this._categoriesUrl)
.map(res => return res.json().data;)
.do(data => console.log(data))
.catch(this.handleError);
}
private handleError(error: Response) {
return Observable.throw(error.json().error || 'Server error');
}
}
它工作得很好,當我使其在客戶端,但是當我使用angular2的通用預覽渲染它在服務器端,它拋出一個錯誤
XMLHttpRequest is not defined
我怎樣才能使它與服務器端渲染?
除了ANSWER JUST提示: - 它能夠更好地提供'HTTP_PROVIDERS'在自舉,而不是在每個部件提供的時間。 –