2017-05-26 65 views
2

我試圖從URL無法從路徑URL

@Component({ 
    templateUrl: './confirm-registration.component.html', 
}) 
export class ConfirmRegistrationComponent implements OnInit { 

    constructor(private route: ActivatedRoute, 
       private router: Router) { } 

    ngOnInit() { 
    this.route.params.switchMap((params: Params) => { 
     return console.log('token:', params['registrationToken']); 
    }); 
    } 

} 

創建我的角CLI項目,負載參數的簡單組件讀取PARAMS,但我得到的錯誤:

無法編譯。

.../confirm-registration.component.ts (15,33): Argument of type '(params: Params) => void' is not assignable to parameter of type '(value: Params, index: number) => ObservableInput<{}>'. Type 'void' is not assignable to type 'ObservableInput<{}>'.

我在4.0.3版本中使用Angular。我犯了什麼錯誤?

+0

你可以試試這個的console.log(this.route.snapshot.params [「registrationToken」]) –

+0

什麼是你的路線,可在req.query –

+1

你正在使用switchMap,它應該返回一個Observable,但你返回調用'console.log'的結果(在這種情況下:void)。請使用'do'操作程序登錄到控制檯進行調試: – dzejdzej

回答

1

我讀取查詢參數與此:

export class ConfirmRegistrationComponent implements OnInit { 

private registrationToken: string; 

constructor(private route: ActivatedRoute) {} 

ngOnInit() { 
this.route.params.subscribe(
    (params) => { 
    this.registrationToken = params['registrationToken']; 
    } 
) 
0

要獲得查詢參數,我用angular-2做了這個。我不知道如果它與角4

constructor(private route: ActivatedRoute) { 
 

 
} 
 

 
ngOnInit() { 
 
    let urParam = this.route.snapshot.queryParams['urParam']; 
 
}

希望工程這有助於

1

由於dzejdzej在他的comment中指出,你必須返回一個可觀察值。這似乎在過去幾個月裏發生了變化(至少它編譯了我幾個月前最後一次嘗試),而且他們還沒有更改文檔。

所以你的情況,你可以簡單地返回相同的觀察到:

ngOnInit() { 
    this.route.params.switchMap((params: Params) => { 
    console.log('token:', params['registrationToken']); 
    return this.route.params; 
    }); 
} 
+0

也許我做了錯誤,但現在出現錯誤: 屬性'switchMap'在類型'Observable '上不存在。 –

+0

您必須從rxjs導入運算符 – mamuso