I have a simple app with the following structure in my main app.html file.
<navigation-list [unreadEmailCount]="unreadEmailCount| async"></navigation-list>
<router-outlet></router-outlet>
In my navigation-list component I can easily create routerLinks as follows:
<a routerLink="/starred" routerLinkActive='active'>link</a>
However, when I go into a component rendered within the router-outlet I cannot use routerLink. Using Router.route.nagive['path'] however, works. Any clues why this is?
EDIT:
App router module:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import {AuthGuard} from "./auth/auth-guard";
import {UnauthGuard} from "./auth/unauth-guard";
const routes: Routes = [
{ path: '', redirectTo: '/inbox', pathMatch: 'full', canActivate: [UnauthGuard] }
];
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
providers: [AuthGuard, UnauthGuard],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
Photos router module (this is where the routerLink is not working)
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import {PhotosComponent} from './photos/photos.component';
import { CreateAlbumComponent } from './album/create-album/create-album.component';
import { AlbumComponent } from './album/album/album.component'
const routes: Routes = [
{ path: 'photos', component: PhotosComponent},
{ path: 'photos/:filter', component: PhotosComponent},
{ path: 'create-album', component: CreateAlbumComponent},
{ path: 'albums', component: AlbumComponent }
];
@NgModule({
imports: [ RouterModule.forChild(routes) ],
exports: [ RouterModule ]
})
export class PhotosRoutingModule {}
EDIT: photosModule:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import {PhotoListComponent} from "./photo-list/photo-list.component";
import {PhotosService} from './shared/photos.service';
import { PhotogroupListComponent } from './photogroup-list/photogroup-list.component';
import { PhotogroupItemComponent } from './photogroup-item/photogroup-item.component';
import { PhotosComponent } from './photos/photos.component';
import {FileUploadComponent} from "./shared/file-upload";
import {FileUploadService} from './shared/file-upload.service';
import { ImageSearchComponent } from './image-search/image-search.component';
import {ImageSearchService} from "./image-search/image-search.service";
import {AlbumSelectComponent} from './album/album-select.component';
import { AlbumSelectBarComponent } from './album/album-select-bar/album-select-bar.component';
import { CreateAlbumComponent } from './album/create-album/create-album.component';
import { CreateAlbumService } from './album/create-album/create-album.service';
import { AlbumService } from "./shared/album.service";
import { AlbumListComponent } from './album/album-list/album-list.component';
import { AlbumItemComponent } from './album/album-item/album-item.component';
import { AlbumComponent } from './album/album/album.component';
import { PhotoNavBarComponent } from './photo-nav-bar/photo-nav-bar.component';
@NgModule({
imports: [
CommonModule,
FormsModule
],
declarations: [
PhotoListComponent,
PhotogroupListComponent,
PhotogroupItemComponent,
PhotosComponent,
FileUploadComponent,
ImageSearchComponent,
AlbumSelectComponent,
AlbumSelectBarComponent,
CreateAlbumComponent,
AlbumListComponent,
AlbumItemComponent,
AlbumComponent,
PhotoNavBarComponent
],
providers: [
PhotosService,
FileUploadService,
ImageSearchService,
CreateAlbumService,
AlbumService
]
})
export class PhotosModule { }
export {PhotosService, FileUploadService, ImageSearchService, CreateAlbumService, AlbumService}
routerLinkActive is simply an indicator of whether this link represents the active route. @jessepinho - Tried it - [routerLinkActive]="'active'" will only work if you also have [routerLink] in the a-tag. If you had (click)="navitageTo('/route')" instead of [routerLink], and in that function, you called this.
Yes it can be attached to div tag, your route is probably wrong try add / in front of route.
What is the difference between [routerLink] and routerLink ? How should you use each one? They're the same directive. You use the first one to pass a dynamic value, and the second one to pass a static path as a string.
If the child component is from a different module then you need to ensure this module has RouterModule
in imports
@NgModule({
imports: [CommonModule, RouterModule],
declarations: [NavigationListComponent],
exports: [NavigationListComponent],
})
export class SomeSharedModule{}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With