我的代码有问题,所以,我需要显示我的项目的详细信息,这些是我从ws获得的详细信息。 我在ws ID上发帖,并且返回给我详细信息。 我试过这段代码:
populateFormAlarm() {
this.route.params.subscribe(
params => {
console.log('params',params) //show id correctly
this.as.AlarmGetById(params['id']).subscribe(
alarm => {
console.log('alarm',alarm) // ID is undefined
this.alarm = alarm;
}
);
}
);
}
Service.ts
public AlarmGetById(id: string): Observable<Alarm> {
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let urlSearchParams = new URLSearchParams();
urlSearchParams.append('id', id);
urlSearchParams.append('unique', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9');
let body = urlSearchParams.toString();
console.log('body', urlSearchParams.toString())
return this.http.post(Api.getUrl(Api.URLS.AlarmGetById), body, {
headers: headers
})
.pipe(map((response: Response) => {
let res = response.json();
if (res.StatusCode === 1) {
} else {
return new Alarm(res.StatusDescription[0]);
}
}));
}
我的参数结果:console.log('params',params)//正确显示id
JS: params {
JS: "id": "5"
JS: }
我的完整错误:
ERROR TypeError: Cannot read property 'id' of undefined
JS: ERROR CONTEXT {
JS: "view": {
JS: "def": {
JS: "nodeFlags": 33603585,
JS: "rootNodeFlags": 33554433,
JS: "nodeMatchedQueries": 0,
JS: "flags": 0,
JS: "nodes": [
JS: {
JS: "nodeIndex": 0,
JS: "parent": null,
JS: "renderParent": null,
JS: "bindingIndex": 0,
JS: "outputIndex": 0,
JS: "checkIndex": 0,
JS: "flags": 33554433,
JS: "childFlags": 49152,
JS: "directChildFlags": 49152,
JS: "childMatchedQueries": 0,
JS: "matchedQueries": {},
JS: "matchedQueryIds": 0,
JS: "references": {},
JS: "ngContentIndex": null,
JS: "childCount": 1,
JS: "bindings": [],
JS: "bindingFlags": 0,
JS: "outputs": [],
JS: "element": {
JS: "ns": "",
JS: "name": "ActionBar",
JS: "attrs": [
JS: [
JS: "",
JS: "class",
JS: "action-bar"
JS: ],
JS: [
JS: "",
JS: "title",
JS: "Details"
JS: ]
JS: ...
你能问我,我在这段代码中使用了params
吗?
我在网络应用中使用的这段代码效果很好。请问我有什么想法,如何解决这个问题。谢谢。
答案 0 :(得分:1)
尝试使用ParamMap,它应该可以运行
let userId = this.route.snapshot.params["id"];
this.as.AlarmGetById(userId).subscribe(
alarm => {
this.alarm = alarm;
}