2017-05-26 70 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