子模块中的Angular 6 routerlink和父模块中的router-out(app.component)

时间:2018-07-27 14:57:35

标签: node.js angular typescript angular6

我正在使用RouterModule,并且routerLinks出现问题。

routerLinks不起作用(锚点不可单击),因为它们位于子模块中,我不知道如何解决此问题。

层次结构:

App.Module(路由器出口)->元素模块-> Navi模块(routerLinks)

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { HttpClientModule } from '@angular/common/http';
import { ElementsModule } from './elements/elements.module';

import { LandingComponent } from './pages/landing.component';
import { GenreComponent } from './pages/genre.component';
import { SinglePostComponent } from './pages/singlepost.component';
import { ListComponent } from './pages/list.component';
import { AppComponent } from './app.component';

import { DataService } from './data.service';

@NgModule({
    imports: [
        BrowserModule,
        ElementsModule,
        HttpClientModule,
        RouterModule.forRoot([
            {path: "", component: LandingComponent},
            {path: "post/:id", component: SinglePostComponent},
            {path: "lists", component: ListComponent}
        ], {
            useHash: true,
        })
    ],
    declarations: [
        AppComponent,
        LandingComponent,
        GenreComponent,
        SinglePostComponent,
        ListComponent
    ],
    providers: [DataService],
    bootstrap: [AppComponent]
})
export class AppModule { }

app.component.html

<router-outlet></router-outlet>

navi.componentent.html

<a routerLink="/movies">Movies</a>

1 个答案:

答案 0 :(得分:0)

在我的项目中,我指定了一个用于处理路线的模块,其他模块可以导入:

app-routing.module.ts:

node.setRight(rightChild);

这是由其他模块导入的,例如:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { MainComponent } from './main/main.component';

const routes: Routes = [
  {
    path: '',
    component: MainComponent
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule {}