2017-07-13 60 views
2

我有一個超鏈接,我需要在Angular 4中創建一個路由器鏈接。我對url有很多部分,其中一部分是數組。我不知道如何讓數組分割成爲routerlink數組的一部分。RouterLink陣列

藉此人爲的例子:

[routerLink]="['/customers', customerid, orderid, arrayofints, anotherint]" 

我想路由器看起來像這樣其中客戶id = 10,爲了= 500,arrayofints = [1,2,3],anotherint = 888

"/customers/10/500/1/2/3/888" 

我結束了這樣的事情,而不是:

"/customers/10/500;0=1;1=2;2=3/888" 
+0

如果你傳播數組:'orderid,... arrayofints,anotherint',會怎麼樣?啊,不:解析器錯誤:意外的令牌。列' – jonrsharpe

+0

@jonrsharpe否定的,解析器錯誤:意外的令牌。在列36 – Darthg8r

+0

如果加入arrayofints會怎麼樣? – Nick

回答

1

壓扁參數會給你りght URL

[routerLink]="[].concat.apply([],['/customers', customerid, orderid, arrayofints, anotherint])" 
1

對於任何不平凡的事情,我會在組件代碼而不是模板中進行工作。所以,在模板:

[routerLink]="customerRouteFor(anotherint)" 

,並在組件,你可以使用...spread語法:

customerRouteFor(anotherint: number): (string | number)[] { 
    return ['/customers', this.customerid, this.orderid, ...this.arrayofints, anotherint]; 
} 

這似乎而非concat方法清潔,在我看來。