2016-12-14 44 views
0

我使用的是angular2 rc.6。我正在開發一種類似於亞馬遜搜索的搜索應用程序,用於顯示產品。 我的問題是,當我路由到不同的參數相同的網址我的組件不會得到更新。但是當我點擊頁面的任何地方或按任意鍵我獲得更新的數據Angular 2 rc.6導航到相同的路線不刷新組件

我的依賴關係是: -

"dependencies": { 
"@angular/common": "2.0.0-rc.6", 
"@angular/compiler": "2.0.0-rc.6", 
"@angular/core": "2.0.0-rc.6", 
"@angular/forms": "2.0.0-rc.6", 
"@angular/http": "2.0.0-rc.6", 
"@angular/platform-browser": "2.0.0-rc.6", 
"@angular/platform-browser-dynamic": "2.0.0-rc.6", 
"@angular/router": "3.0.0-rc.2", 
"@angular/upgrade": "2.0.0-rc.6", 
"systemjs": "0.19.27", 
"core-js": "^2.4.1", 
"reflect-metadata": "^0.1.3", 
"rxjs": "5.0.0-beta.11", 
"zone.js": "^0.6.17", 
"angular2-in-memory-web-api": "0.0.18", 
"bootstrap": "^3.3.6" 

},

我的路由配置是: -

 const searchRoutes: Routes = [{ path: 'home',component: Dashboard,}, 
{ path: 'titleList/:titleName',component: TitlesListDetails},] 
    export const searchRouting = RouterModule.forRoot(searchRoutes); 

路線時使用以下部件

​​

最後TitlesListDetails成分: -

constructor(private searchService : SearchService, 
       private route: ActivatedRoute, 
       private router: Router){ 
    this.route.params.subscribe(params => { this.searchService.fetchAllSearchData(params['titleName'],localStorage.getItem("sortTerm")).subscribe(searchResult => { 
       this.searchResult = searchResult; 
       this.products=searchResult.products;    
      },error => console.log("Error while fetching Products Details") 
     );   
     }); 
    } 

我在構造函數中訂閱的路由參數。 任何機構可以建議如何更新TitlesListDetails

回答

2

當你正在做的組件,重新加載PARAMS在組件的最好方法是使用類似:

this.route.params.subscribe(...) 

這裏有一個問題,開解釋這個選擇:https://github.com/angular/angular/issues/9811

它適用於我與Angular 2.2.4和路由器3.2.4,也許你必須更新?

package.json文件的摘錄:

"dependencies": { 
    "@angular/common": "2.2.4", 
    "@angular/compiler": "2.2.4", 
    "@angular/core": "2.2.4", 
    "@angular/forms": "2.2.4", 
    "@angular/http": "2.2.4", 
    "@angular/platform-browser": "2.2.4", 
    "@angular/platform-browser-dynamic": "2.2.4", 
    "@angular/router": "3.2.4", 
    "bootstrap": "3.3.6", 
    "rxjs": "5.0.0-rc.4", 
    "ts-helpers": "^1.1.1", 
    "zone.js": "0.7.1" 
}, 
+0

感謝答覆。我會檢查 – Vicky3D

+0

你能分享一下package.json文件嗎? – Vicky3D

+0

我編輯我的答案與提取 –