【问题标题】:RouterLink not working in child componentsRouterLink 在子组件中不起作用
【发布时间】:2017-04-24 00:12:25
【问题描述】:

我的主 app.html 文件中有一个具有以下结构的简单应用程序。

    <navigation-list [unreadEmailCount]="unreadEmailCount| async"></navigation-list>
    <router-outlet></router-outlet>

在我的导航列表组件中,我可以轻松地创建 routerLinks,如下所示:

<a routerLink="/starred" routerLinkActive='active'>link</a>

但是,当我进入路由器插座中呈现的组件时,我无法使用 routerLink。但是,使用 Router.route.naive['path'] 可以。有什么线索吗?

编辑:

应用路由模块:

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

照片路由器模块(这是 routerLink 不起作用的地方)

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

编辑:照片模块:

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}

【问题讨论】:

  • 添加你的路由配置,你在你的子组件中也添加了&lt;router-outlet&gt;,添加它的代码以及帮助。
  • 感谢您的建议。我在上面添加了路由器模块

标签: angular router


【解决方案1】:

如果子组件来自不同的模块,那么您需要确保该模块在imports 中有RouterModule

@NgModule({
  imports: [CommonModule, RouterModule],
  declarations: [NavigationListComponent],
  exports: [NavigationListComponent],
})
export class SomeSharedModule{}

【讨论】:

  • 我用上面的两个路由器模块进行了编辑。据我所知,它们已经包含在内,但我可能遗漏了一些东西
  • “不能使用routerLink”到底是什么意思?
  • 这意味着这段代码 link 甚至没有评估为链接(即光标不会变为指针),当我点击时什么也没有发生它
  • 但是您在浏览器控制台中没有收到任何错误?此行为表明 routerOutlet 指令未知,应通过我的回答修复:-/
  • NavigationList 组件属于哪个 NgModule?
猜你喜欢
  • 2018-06-06
  • 1970-01-01
  • 2018-11-25
  • 1970-01-01
  • 2021-03-30
  • 1970-01-01
  • 1970-01-01
  • 2016-12-18
  • 2020-01-30
相关资源
最近更新 更多