首页 > 解决方案 > 在 api 请求角度中添加变量

问题描述

我有一个以 id 作为参数的函数并执行以下操作:

onChooseVehicle(id){

      console.log(id);

      this.router.navigate(['tracking/v/-- id must be here --/live'])

  }

id 的输出:4

这意味着我已成功接收数据,我如何将这个 id 放在 url 中?

      this.router.navigate(['tracking/v/ --HERE -- /live'])

标签: angularapi

解决方案


您可以使用模板文字

this.router.navigate([`tracking/v/${id}/live`]);  // notice the back ticks `` instead of quotes ''

或字符串连接

this.router.navigate(['tracking/v/' + id + '/live']);  // here using quotes ''

你不能混合它们。


推荐阅读