2016-05-31 36 views
1

最初,AppComponent有2個父項@Routes(LoginComponent & TodosComponent)。 LoginComponent沒有嵌套@Routes,TodosComponent有2個嵌套@Routes。我已點擊href Todos鏈接進入todoslist頁面。我能夠使用瀏覽器後退按鈕導航回登錄頁面。如果通過點擊todoslist頁面進入todosdetail頁面,那麼我可以導航回todoslist頁面,但我無法使用瀏覽器後退按鈕導航回登錄頁面。返回導航無法從父項2到父項1嵌套@Routes - Angular 2 rc 1 - TypeScript

main.ts: -

import {bootstrap} from "@angular/platform-browser-dynamic"; 

import {ROUTER_PROVIDERS} from "@angular/router"; 

import {ROUTER_PROVIDERS as _ROUTER_PROVIDERS} from "@angular/router-deprecated"; 

import {Title} from "@angular/platform-browser"; 

import {HTTP_PROVIDERS} from "@angular/http"; 

import {AppComponent} from "./app.component"; 

import {MyService} from "./services/home"; 

bootstrap(AppComponent, [ROUTER_PROVIDERS, _ROUTER_PROVIDERS, Title, HTTP_PROVIDERS, MyService]); 

home.ts(爲MyService): -

import {Injectable, EventEmitter} from "@angular/core"; 
import {Subject} from "rxjs/Subject"; 

export class Todos{ 
    constructor(public id: number | string){ 

    } 
} 

@Injectable() //used for injecting Http or other predefined services for the following class 
export class MyService{ 
    loginEmitter: EventEmitter<any> = new EventEmitter() 

    stopTimerSource = new Subject() 
    stopTimer$ = this.stopTimerSource.asObservable() 
    constructor(){ 

    } 
    loginEmitInit(){ 
     return this.loginEmitter; 
    } 
    loginEmit(data){ 
     this.loginEmitter.emit(data); 
    } 
    stopTimer(){ 
     this.stopTimerSource.next(""); 
    } 
} 

app.component.ts: -

import {Component, ViewChild} from "@angular/core"; 
import {ROUTER_DIRECTIVES, Routes, Router} from "@angular/router"; 
import {Router as _Router} from "@angular/router-deprecated"; 
import {Http} from "@angular/http"; 
import {Location} from "@angular/common"; 
import {Title} from "@angular/platform-browser"; 

import {LoginComponent} from "./components/login"; 
import {TodosComponent} from "./components/todos"; 

import {MyService} from "./services/home"; 

@Component({ 
    selector: "my-app", 
    template: "<a [routerLink]="['/login']">Login</a>" + 
       "<a [routerLink]="['/todos']">Todos</a>" + 
       "<router-outlet></router-outlet>", 
    directives: [ROUTER_DIRECTIVES] 
}) 

@Routes([ 
    {path: "/login", component: LoginComponent}, 
    {path: "/todos", component: TodosComponent}, 
    {path: "*", component: LoginComponent}, 
]) 

export class AppComponent { 
    loginSubscription: any 
    constructor(myService: MyService){ 
     this.loginSubscription = myService.loginEmitInit().subscribe(function (data) { 
      //do something 
     }); //event emit subscription 
    } 
} 

個login.ts(LoginComponent): -

import {Component} from "@angular/core"; 
import {Router} from "@angular/router"; 

import {MyService} from "../services/home"; 

@Component({ 
    template: "<span>Login</span>", 
    directives: [] 
}) 

export class LoginComponent { 
    stopTimerSubscription: any 
    constructor(private router: Router, private myService: MyService){ 
     this.stopTimerSubscription = myService.stopTimer$.subscribe(()=>{ 
      //do something 
     }); 
    } 
    ngOnDestroy(){ 
     this.stopTimerSubscription.unsubscribe(); 
    } 
} 

todos.ts(TodosComponent): -

import {Component} from "@angular/core"; 
import {ROUTER_DIRECTIVES, Routes} from "@angular/router"; 

import {TodosListComponent} from "../components/todoslist"; 
import {TodosDetailComponent} from "../components/todosdetail"; 

@Component({ 
    template: "<router-outlet></router-outlet>", 
    directives: [ROUTER_DIRECTIVES] 
}) 

@Routes([ 
    {path: "", component: TodosListComponent}, 
    {path: "/:id", component: TodosDetailComponent}, 
]) 

export class TodosComponent {} 

todoslist.ts(TodosListComponent): -

import {Component} from "@angular/core"; 
import {Router, RouteSegment} from "@angular/router"; 

import {Todos} from "../services/home"; 

@Component({ 
    template: "<span (click)='onTodosDetailNav()'>Todos List</span>", 
    directives: [] 
}) 

export class TodosListComponent { 
    constructor(){private router: Router} 
    routerOnActivate(curr, prev, currTree, prevTree){ 
     let selectedId = +currTree.parent(curr).parameters.id; 
    } 
    routerCanDeactivate(curr, next){ 
     return true; //return false to cancel navigation to next page 
    } 
    onTodosDetailNav(){ 
     this.router.navigateByUrl("/todos/1"); 
    } 
} 

todosdetail.ts(TodosDetailComponent): -

import {Component} from "@angular/core"; 
import {Router} from "@angular/router"; 

import {Todos} from "../services/home" 

@Component({ 
    template: "<h1>{{todosDetail.id}}</h1>" + 
       "<button (click)='goBack()'>Go Back</button>"  
}) 

export class TodosDetailComponent{ 
    todosDetail:Todos 
    constructor(private router: Router){ 
     this.todosDetail = {id:"1"}; 
    } 
    goBack(){ 
     this.router.navigate(['/todos', {id:this.todosDetail.id, foo:"foo"}]); //sending query parameters in url path 
    } 
} 

有人可以幫我解決這個問題嗎?提前致謝。

+1

請在此處發佈代碼並刪除指向Dropbox的鏈接。 –

+0

@JonathanEustace發佈代碼。你能檢查它並給我解決方案嗎? –

回答

0

我覺得經過id應該以這種方式工作

goBack(){ 
    this.router.navigate(['/todos', this.todosDetail.id, {foo:"foo"}]);   
} 

但查詢參數({foo:"foo"})尚不支持在新的路由器據我所知。

+0

我嘗試過使用[Router](https://angular.io/docs/ts/latest/guide/router.html)。當我回去時,我需要選擇待辦事項列表內容。所以我傳遞了查詢參數。我有URL作爲** http:// localhost:3000/todos/1 **在todos細節。如果我傳遞'this.router.navigate(['/ todos',this.todosDetail.id,{foo:「foo」}])'並點擊返回按鈕,我會將url作爲** http: //本地主機:3000 /待辦事項/ 1;富= FOO **。現在,如果我點擊瀏覽器後退按鈕,它將** ** http:// localhost:3000/todos/1 **(仍然在todos詳細信息中)。這是因爲我在路由中配置爲'「/:id」'。 –

+0

這是一個問題或只是一些反饋?抱歉,不知道如何處理此評論。 –

+0

我已經和你一起嘗試過,並且我在todos詳細信息頁面中停下了腳步。在發佈的代碼中,一切正常。但我不能回去使用從todos列表瀏覽器後退按鈕登錄,當我去todos詳細信息頁面。我得到了[鏈接]中的錯誤(https://www.dropbox.com/s/n9bvbm8hn1salw5/nav-error.png?dl=0)。我需要知道爲什麼我得到這個錯誤,爲什麼我沒有能夠導航回登錄後去了todos的細節。 –