2017-07-30 39 views
0

我想更改組件中currentUrl對象的id屬性。我想實現的是一旦id被更改,它應該從id中獲取適當的數據。我目前正在得到下面的錯誤。如何更改角2中的currentUrl ID?

無法指定讀取對象的唯一屬性'身份證‘的翻譯:’

任何人可以幫助我解決這個問題?

addBranch(id){ 
     this.currentUrl = this.activatedRoute.snapshot.params; 
     console.log(this.currentUrl.id); // Currently the id is undefined 
     this.currentUrl.id = id; //Cannot assign to read only property 'id' of object '[object Object]' 
     window.location.reload(); 
} 

回答

1

你可以通過有角度的路由器來做到這一點。首先,將它注入你的組件:

constructor(private router: Router) 
{} 

(如果你已經有一個現有的構造函數,相應地擴展它)。

現在你可以使用this.router.navigate(['/your-desired-route', id]);,而不是

this.currentUrl.id = id; 
window.location.reload(); 

這不會導致重新加載整個頁面,而只是設置正確的URL並觸發所需處理路徑變化(如確定正確的事情組件來處理請求),從而避免了角度的不必要的重新初始化。

+0

感謝哥們!有效!! –