1

The AppComponent has 2 Parent @Routes(LoginComponent & TodosComponent) initially. LoginComponent has no nested @Routes, TodosComponent has 2 nested @Routes. I have gone to todoslist page by clicking on href Todos link. I am able to navigate back using browser back button to login page. If I will go to todosdetail page by clicking on todoslist page, after that I am able to navigate back to todoslist page, but I am not able to navigate back to login page using browser back button.

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
    }
}

Can someone help me to solve this issue? Thanks in advance.

Abraham Gnanasingh
  • 837
  • 2
  • 10
  • 31

1 Answers1

0

I think passing the id should work this way

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

but query parameters ({foo:"foo"}) are not yet supported in the new router as far as I know.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • i have tried using this [Router](https://angular.io/docs/ts/latest/guide/router.html). i need to select the todos list content when i go back. so I am passing the query parameters. i have url as **http://localhost:3000/todos/1** in todos detail. if I am passing `this.router.navigate(['/todos', this.todosDetail.id, {foo:"foo"}])` and clicking Go Back button in todos detail, i get url as **http://localhost:3000/todos/1;foo=foo**. now if i click browser back button, it is going to **http://localhost:3000/todos/1** (still in todos detail). this is because i configured in routes as `"/:id"`. – Abraham Gnanasingh Jun 01 '16 at 14:16
  • Is this a question or just some feedback? Sorry, not sure what to do with this comment. – Günter Zöchbauer Jun 01 '16 at 14:42
  • i have tried with yours and i got stopped in the todos detail page itself. in the posted code, everything works fine. but i can't go back using browser back button from todos list to login when i gone to todos detail page. i got the error which is in the [link](https://www.dropbox.com/s/n9bvbm8hn1salw5/nav-error.png?dl=0). i need to know why i am getting this error and why i am not able to nav back to login after gone to todos detail. – Abraham Gnanasingh Jun 01 '16 at 15:03
  • Can you create a Plunker that allows to reproduce? In Plunker editor got to `new` > `Angularjs` > `2.0.x (TS)` to get a template. – Günter Zöchbauer Jun 01 '16 at 15:11
  • sorry for the delay. i just created my first plunker. [https://plnkr.co/edit/BEzo8eH4b4NChHKZppGd?p=preview](https://plnkr.co/edit/BEzo8eH4b4NChHKZppGd?p=preview) – Abraham Gnanasingh Jun 01 '16 at 16:13
  • in the plunker, i don't see any error coming. that i don't know why. but i am getting the error which i sent you in the [link](https://www.dropbox.com/s/n9bvbm8hn1salw5/nav-error.png?dl=0). in the plunker, you can see i am not able to nav back to login after gone to todos detail – Abraham Gnanasingh Jun 01 '16 at 16:20
  • I can reproduce the broken back/forward behavior (not the exception) but don't know what's wrong. You must not mix `@angular/router` and `@angular/router-deprecated`. Use either one or the other consistently throughout your whole application. Fixing that didn't fix back/forward behavior though. – Günter Zöchbauer Jun 01 '16 at 16:26
  • i have removed `@angular/router-deprecated` & updated the plunker. still i am not able to nav back to login after gone to todos detail. in the given [Router](https://angular.io/docs/ts/latest/guide/router.html) live example also, the same scenario is not working(after gone to crisis detail). did u checked that live example? i think this issue is in angular router and they may fix in sometime – Abraham Gnanasingh Jun 01 '16 at 16:36
  • Yes, that what I could reproduce. I don't know of such an issue but I don't know all by heart ;-). Waiting for the next release would make sense. – Günter Zöchbauer Jun 01 '16 at 16:37
  • ok perfect. can you look up my other [issue](http://stackoverflow.com/questions/37287510/viewchild-not-working-in-angular-2-rc-1-javascript) and give me a solution regarding `@ViewChild` ? – Abraham Gnanasingh Jun 01 '16 at 16:44