0

Trying to create a SPA with angular2:

Inside my main module I declare the routes and the components that I am about to use: mainApp.ts:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule, Routes }  from '@angular/router';
import { BaseComponent,IndexComponent, ContactComponent, 
         PortfolioComponent, AboutComponent }  from './BaseComponents';

const appRoutes: Routes = [
  { path: '', component: IndexComponent },
  { path: 'about', component: AboutComponent },
  { path: 'portfolio', component: PortfolioComponent},
  { path: 'contact', component: ContactComponent}
];

@NgModule({
  imports:      [ BrowserModule, RouterModule.forRoot(appRoutes)],
  declarations: [ BaseComponent, IndexComponent, ContactComponent, 
                  PortfolioComponent, AboutComponent ],
  bootstrap:    [ BaseComponent ]
})
export class AppModule { }

So, it's clear that I bootstrap the BaseComponent So inside the BaseComponent.ts I import the followings:

import { Component } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { IndexComponent, AboutComponent, PortfolioComponent, 
         ContactComponent} from '../BaseComponents';

and inside the template:

<a [routerLink]='index'>index</a>
<a [routerLink]='portfolio'>portfolio</a>   
<a [routerLink]='about'>about</a>
<a [routerLink]='contact'>contact</a>

<router-outlet></router-outlet>

If I type manualy inside the url, then I do navigate to the upper paths. But if I click on them, no reaction.. Debugger doesn't give any error-warning.

Any help is welcome!

1
  • Replacing like: <a [routerLink]='[index]'>index</a> etc, gives me undefined imgur.com/a/EBmGc Commented Mar 26, 2017 at 12:52

1 Answer 1

3

when you use [routerLink]="index" index must be a property of your component.

<a [routerLink]="'index'">index</a>
<a [routerLink]="'portfolio'">portfolio</a>   
<a [routerLink]="'about'">about</a>
<a [routerLink]="'contact'">contact</a>

or

<a routerLink="index">index</a>
<a routerLink="portfolio">portfolio</a>   
<a routerLink="about">about</a>
<a routerLink="contact">contact</a>

or

<a routerLink='index'>index</a>
<a routerLink='portfolio'>portfolio</a>   
<a routerLink='about'>about</a>
<a routerLink='contact'>contact</a>
Sign up to request clarification or add additional context in comments.

1 Comment

yes, this is it. It wasn't passed like a property. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.