添加相对路径时出现编译错误。
import { Component, OnInit } from '@angular/core';
import { ServersService } from '../servers.service';
import { ActivatedRoute, Params, Router } from '@angular/router';
constructor(private serversService: ServersService, private route: Router, private acRoute: ActivatedRoute) { }
onEditServer()
{
this.route.navigate(['edit'], {relativeTo: this.route});
}
这是{relativeTo: this.route}
上的错误。
错误说明:
error TS2345: Argument of type '{ relativeTo: Router; }' is not assignable to parameter of type 'NavigationExtras'.
Types of property 'relativeTo' are incompatible.
Type 'Router' is not assignable to type 'ActivatedRoute'.
Property 'params' is missing in type 'Router'.
我在哪里错了?
答案 0 :(得分:2)
您需要在第二个参数中使用 acRoute
代替 route
onEditServer() {
this.route.navigate(['edit'], { relativeTo: this.acRoute});
}