角打字稿连接数字而不是添加

Yas*_*otu 1 typescript angular angular4-router

我有3个用户,当我单击“下一步”时,它必须为下一个用户加载路由,所以我将一个添加到ID并传递给routerLink,但以某种方式而不是添加它来连接数字,以下是代码

import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute,Params } from '@angular/router';
import { Subscription } from 'rxjs/Subscription';
@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit,OnDestroy {
  routeSubscription : Subscription;
  id : number;
  next :  number = 0;
  constructor(private route:ActivatedRoute) { 
  }

  ngOnInit() {
  this.routeSubscription =  this.route.params.subscribe((params :Params) =>{
    this.id = params['id'];
    this.next = this.id  + 1;
  });
  }
  ngOnDestroy(){
    this.routeSubscription.unsubscribe();
  }
}
Run Code Online (Sandbox Code Playgroud)

HTML模板

<p>
  user id : {{ id }}
</p>

<button class="btn btn-primary" [routerLink] = "['/Users', next ]">Next</button>
Run Code Online (Sandbox Code Playgroud)

请让我知道为什么下一个要与id串联

Jea*_* A. 5

问题是params对象返回的id的值this.id = params['id'];是一个字符串值。

以下应解决您的问题

this.next = +this.id  + 1; // The id is cast to a number with the unary + operator
Run Code Online (Sandbox Code Playgroud)