0
我以爲我會試用一點與SEO使用Angular4/Node.js,但已經來過一個挑戰。客戶端異步。請求與服務器端渲染
Angular Universal使我有可能做服務器端渲染,所以我可以讓我的元關鍵字,標題,圖像網址等注入到HTML之前,我的網頁瀏覽器 - 很棒!現在的挑戰是,在某些時候,我希望至少能夠在客戶端異步執行一些東西,例如。獲取一些JSON數據,甚至捕獲一個Angular click事件。
我似乎無法做到這樣的東西,而使用下面的例子。
home.component.ts
import { Component, OnInit } from '@angular/core';
import { Http, Response } from '@angular/http';
@Component({
selector: 'home',
template: `<button (click)="fetch();">Fetch some json</button>`,
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
constructor(
private _http: Http
) {}
ngOnInit() {}
public fetch(){
console.log("Clicked"); // Does not happen
this._http.get('./i18n/da.json')
.subscribe((r: Response) => {
console.debug(r.json()); // Also not happening
});
}
}
有什麼沒有辦法像這樣使用通用的服務器端渲染時將有可能?你們會如何執行此操作?
乾杯!
3.是正確的答案:)。我的軟件包未包含在應用程序中,並且未產生任何錯誤。感謝你說它應該按照預期工作,那是什麼使我朝着正確的方向發展。 – iMe