2

我收到一个错误

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

0 回答 0