2

I am researching about the feature which let us to change the page in an angular2 project.

I got to route different components in the same page by demand. But I want now, to create an hyperlink which redirect me to another page. I can´t figure it out...

Honestly, I am completely lost. I can´t almost find information about it.

My best approaches were (shame on me) trying to use button instead of hiperlink html tag:

my_component.ts

<button (click)="redirect()">Socios del VideoClub</button>
....
Export class myComponent{

    constructor(
     private route: ActivatedRoute,
     private router: Router
    ) {}

    redirect(){
        this.router.navigate(['/socios']);
    }

I defined '/socios' in my app.routing.ts. I tried with navigationByUrl, as well.

Both of them, just do the same, render it in the same page.

Note: I want to redirect by component (I´m injecting in the component), so I can´t use window.location.href

Thanks mates

[EDITED]

app.routing.ts

// Importar componentes y módulos para el routing 
import { Routes, RouterModule } from '@angular/router';

// Componentes
import { SociosComponent } from './socios.component';

// Configuración de las rutas
const appRoutes: Routes = [
  { path: 'socios', component: SociosComponent }
];

export const appRoutingProviders: any[] = [
];

export const routing = RouterModule.forRoot(appRoutes);

Note2: I coded an index. It is a bootstrap carousel fullscreen.

The caption for each image, will be a link to another page.

I want to use angular´s routing for going to these different pages.

I achieved to render a component from the same page which is called it.

I can render, using just html hypelink tag, another static page.

But my problem is when I want to render another page using angular. I want to do it, because I am injecting the model, coming from a service, in this component...

Note3: Well, finally, I did a plunker. My first time doing it so I hope all is correct. If not, please, let me know what you all need and I Will change it. Thanks again :):)

Demo plunker

Kenzo_Gilead
  • 2,187
  • 9
  • 35
  • 60
  • Can you add your routing modules? – eko Jan 19 '17 at 16:49
  • Sure @echonax. Thanks for your quickly answer. – Kenzo_Gilead Jan 19 '17 at 16:51
  • 1
    So the button click just refreshes the page of `myComponent` ? – eko Jan 19 '17 at 16:55
  • 1
    Not, the button click just renders the component between router-outlet tags. – Kenzo_Gilead Jan 19 '17 at 16:56
  • 2
    I really didn't understand the situation, isn't that the intended behaviour of router-outlet :-)? Can you reproduce the issue on a plunker? – eko Jan 19 '17 at 16:59
  • 2
    I really suggest that you read the tutorial https://angular.io/docs/ts/latest/tutorial/toh-pt5.html usage of `routerLink` is probably what you want and not the button, since you mentioned that "shame on you" for not using a hyperlink. Just as a side comment. :) – AT82 Jan 19 '17 at 17:01
  • Hhaha. Sorry @echonax, just a beginner. It could be, but could you give any sugestion for render another page instead of render a component in the same page... I want when I click the hyperlink, or button, another page appears. This new page should be call by component. Do you get me? – Kenzo_Gilead Jan 19 '17 at 17:04
  • Hi @AJT_82, thanks :). In am using this tuto. In my first approach I used them, but I can´t achieve the functionality I told you both... – Kenzo_Gilead Jan 19 '17 at 17:06
  • Well did you check out the plunker there? I mean it's all there what you need :) Check the specially the files app.component, dasboard.component and heroes.component, herodetail component in the plunker. There is use of routerLink and how navigating. Try it and then come back if you have some specific issue and present your code, preferably with a plunker :) We want to see what you have tried. That's my advice :) Alternatively you could try and reproduce your issue now, either way :) – AT82 Jan 19 '17 at 17:15
  • Ok @AJT_82. I will do it. But I am looking for another feature than the plunker. I used this tuto before, and I got to code something similar... Anyway, I will read it again, from my ignorence, maybe I am loosing something what you all are seeing clear... :). Thanks mate. Have a good day :) :) – Kenzo_Gilead Jan 19 '17 at 17:21
  • Yes, from what you edited your post I understand you need some other feature. By the code you have provided, we can't guess how your code looks or what you have tried. We just see what you "need", but without seeing relevant code we really can't do anything! ;) Have a good day too! – AT82 Jan 19 '17 at 17:24
  • remove '/' from this.router.navigate(['socios']); – JSNinja Jan 19 '17 at 17:30
  • Hi @JSNinja, thanks for your answer. With it, I will use just the routerLink way, which achieve the same functionality which I don´t want. – Kenzo_Gilead Jan 19 '17 at 17:38
  • 1
    Well, finally, I did a plunker. My first time doing it o I hope all is correct. If not, please, let me know what you all need and I Will change it. Thanks again :):) https://plnkr.co/edit/FGPEwcKD4gKQn5RIJphF?p=catalogue @AJT_82 – Kenzo_Gilead Jan 19 '17 at 17:40
  • On plunker there is a template for making Angular 2 plunkers, I suggest you use them, so that you have the correct configuration so app will run. Looked at your plunker, make a app.component that has routerLinks and router-outlet. Look at the Angular tutorial, make a app.component identical like there in the plunker. Then the current content you have in the appcomponent, make it a new component. Then your routing will work as you want! – AT82 Jan 19 '17 at 18:48

1 Answers1

2

Here are some guidelines for getting your routing to work as you wish. Referring to your plunker The current content in the app.component should be a separate component. The content you currently have in your app.component should look something like this:

@Component({
  selector: 'my-app',
  template: `
    <nav>
      <a routerLink="/socios">Socios</a>
    </nav>
    <router-outlet></router-outlet>
  `
})
export class AppComponent {
}

And the current content in the app.component, as said, make a new component. Let's call it "LandingPage" for now.

Then in your routes you can define that this component will be the landing page when user navigates to your app, like so: (app.routing)

import { Routes, RouterModule } from '@angular/router';
import { SociosComponent } from './socios.component';
import { LandingPage }  from './pathHere' // your "new" component

const appRoutes: Routes = [
  { path: '', redirectTo: 'landing', pathMatch: 'full' }, // this will make your component the landing page!
  { path: 'landing', component: LandingPage }
  { path: 'socios', component: SociosComponent }
];

export const routing = RouterModule.forRoot(appRoutes);

So this would make your routing work as you want, meaning when you click the hyperlink, you will be routed to a new page! :) As said, check the tutorial and the plunker on

Tour of Heroes - Routing

There you can see the same setup, having a different app component with routerlinks and router-outlet, separate from the rest of your components!

EDIT: Working Plunker

Community
  • 1
  • 1
AT82
  • 71,416
  • 24
  • 140
  • 167
  • Wow. I am really tired man, studying all day, but you efford deserve, at least, my vote for your answer. I will check your solution, and I will accept it in the next days. Thank you so much champ ;). Cool man! :P – Kenzo_Gilead Jan 19 '17 at 20:20
  • 1
    No worries! This should really solve the issue though. Having your routeroutlet where you have it now causes this behavior that you do not get routed to a new page :) – AT82 Jan 19 '17 at 20:22
  • 1
    I bet it will... :). In a few days I will come back for saying thank you again. Cheers mate :) :) – Kenzo_Gilead Jan 19 '17 at 20:27
  • 1
    Hey man, felt soooooooo kind today, so I made you a working plunker :D Check the last part of my answer ;) – AT82 Jan 19 '17 at 21:19
  • Well, I just put a few beers in the freezer (with your name). LOL man, thank you so much. I checked the plunker and it works, so... Thanks again. This morning I will implemented in my demo... Awesome. Thanks mate... Could I have your linkedin or email? I would like to be in touch with you... :) :) :) – Kenzo_Gilead Jan 20 '17 at 07:06
  • 1
    Cool @AJT_82. I coded another approach for the nav, I want it, but has to appear after landing page. So, a setting it by display:none, and changing it value in the others pages... Angular2 JS is other paradigma for me, Component and so on... Enjoying learning it and happy to met you at this time. Have a good weekend ;) – Kenzo_Gilead Jan 20 '17 at 09:13
  • 1
    You have great weekend too and happy coding! :) – AT82 Jan 20 '17 at 09:20
  • Thank you for the Plunker. I created it locally (exact copy of your files) and it works. But when I hit the browser's refresh button (or Ctrl-R) I get a 404. I added this route but that didn't help: { path: '**', redirectTo: 'landing' } What could be missing? Thanks again. – Jelgab Apr 20 '17 at 11:28
  • @Jelgab Hmm strange, you did add the `path: '**'` as the last in the routing (this is important)? The routing in this answer should work, at least the similar setup works fine for me... (with refreshing). – AT82 Apr 20 '17 at 11:41
  • I must be missing something simple. Just in case you want (and have time) to take a look, I published your example to http://joselillo.pro/ (with the refresh issue) and put the files in https://github.com/Jelgab/Ang2RoutingEx . :-) – Jelgab Apr 22 '17 at 12:08
  • 1
    @Jelgab Tested, locally this works, but when you deploy this app you need to do some more Angular magic :P This should help: http://stackoverflow.com/questions/35284988/angular-2-404-error-occur-when-i-refresh-through-browser – AT82 Apr 22 '17 at 12:27
  • 1
    @Jelgab Also check the dupe for that question :) – AT82 Apr 22 '17 at 12:29
  • @AJT_82, I followed your links, used [HashLocationStrategy] as it says there, and everything worked fine. I will also test the proposed solution (server side) for core. Thank you for your help. – Jelgab Apr 27 '17 at 01:08
  • @Jelgab Great, glad to hear I could help a little! :) – AT82 Apr 27 '17 at 06:52