gpt4 book ai didi

angular - RouterLink 无法在子组件中工作

转载 作者:行者123 更新时间:2023-12-02 17:11:38 24 4
gpt4 key购买 nike

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

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

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

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

但是,当我进入路由器导出内呈现的组件时,我无法使用 routerLink。然而,使用 Router.route.nagive['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 {}

编辑: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}

最佳答案

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

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

关于angular - RouterLink 无法在子组件中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41044772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com