-1

我的代码有问题,所以,我需要显示我的项目的详细信息,这些详细信息是从我的 ws 中获得的。我在 ws ID 中发帖,然后 ws 将详细信息返回给我。我试过这段代码:

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

服务.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在这段代码中使用正确吗?

我在网络应用程序中使用的这段代码效果很好。请问我有什么想法,如何解决这个问题。谢谢你。

4

1 回答 1

1

尝试使用 ParamMap 并且它应该可以工作

    let userId = this.route.snapshot.params["id"];
    this.as.AlarmGetById(userId).subscribe(
            alarm => {
                this.alarm = alarm; 
            }
于 2018-06-13T20:06:54.043 回答