类型' ActivatedRoute'的论点不能分配给' ActivatedRoute'类型的参数。

时间:2017-03-15 09:49:13

标签: angular typescript routes observable canactivate

我收到错误

Type' ActivatedRoute'不能分配给' ActivatedRoute'。

类型的参数

财产类型' url'是不相容的。 输入' Observable'不能分配给' Observable'。存在两种具有此名称的不同类型,但它们是不相关的。 财产来源'受到保护,但键入' Observable'不是来自' Observable'。

的类

我的打字稿如下......我在navigateToList()

中遇到错误
import { Component, ViewChild, OnInit } from '@angular/core'
import { Router, ActivatedRoute } from '@angular/router'
import { getParentRoute } from 'PwcLib/common'
import { ValidatorsService } from 'PwcLib/core-services'
import { ValidationForm } from 'PwcLib/validation'
import { CanDeactivateComponent } from 'PwcLib/routing'
import { UsersSyncService } from '../users.service'
import { User } from '../user'

@Component({
// define a templateUrl for component
templateUrl: 'user-details.component.html'
})
export class UserDetailsComponent implements OnInit, CanDeactivateComponent
{
user: User;
userId: number;
@ViewChild(ValidationForm) form: ValidationForm;

constructor(
    private usersSyncService: UsersSyncService,
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private validators: ValidatorsService
) {

}

ngOnInit() {
    //ngOnInit is not executed on parameters change so supscribe to params
    this.activatedRoute.params.forEach((params) => {
        let userIdParam = parseInt(params['userId']);
        this.userId = isNaN(userIdParam) ? 0 : userIdParam;
        if (this.userId == 0) {
            this.user = <User>{};
        } else {
            this.user = this.usersSyncService.getUser(this.userId);
            if (!this.user) {
                this.navigateToList();
            }
        }
    });
}

canDeactivate() {
    if (!this.form) return true;

    return this.form.allowNavigation();
}

private navigateToList() {
    // ../ navigates one level up in route but route can have few parts like /details/:id so getParentRoute utility is used to get parent route
    this.router.navigate(['./'], { relativeTo: getParentRoute(this.activatedRoute) });
}
}

0 个答案:

没有答案