类型'{的相对论点:路由器; }”不可分配给“ NavigationExtras”类型的参数

时间:2018-08-23 08:54:46

标签: angular typescript angular2-routing

添加相对路径时出现编译错误。

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'.

我在哪里错了?

1 个答案:

答案 0 :(得分:2)

您需要在第二个参数中使用 acRoute 代替 route

  onEditServer() {
    this.route.navigate(['edit'], { relativeTo: this.acRoute});
  }