如何将子组件路由到另一个子组件

问题描述 投票:0回答:2

我的项目中有两个子组件google和dpikachu。

我需要将谷歌页面(当我点击下一个按钮时)路由到另一个子组件皮卡丘。

这是我的app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { PikachuComponent } from './pikachu/pikachu.component';
import { GoogleComponent } from './google/google.component';
import {HomeComponent} from './home/home.component';

const routes: Routes = [
  { path: 'google', component: GoogleComponent },
  { path: 'pikachu', component: PikachuComponent  },
  {path: '', component: HomeComponent},
];

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

这是我的app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {FormsModule} from '@angular/forms';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { PikachuComponent } from './pikachu/pikachu.component';
import { GoogleComponent } from './google/google.component';
import { HomeComponent } from './home/home.component';

@NgModule({
  declarations: [
    AppComponent,
    PikachuComponent,
    GoogleComponent,
    HomeComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

请帮忙

angular routing components
2个回答
0
投票

你可以尝试这样做。

import { ActivatedRoute, Router  } from '@angular/router';

constructor(private route: ActivatedRoute, private router: Router) {}

navigateToChild(){
   this.router.navigate(['./pikachu'], { relativeTo: this.route });
}

然后

 <a target="_blank" rel="noopener" (click)="navigateToChild()"> <button class="btn-primary">Next</button></a>

0
投票

你有没有将AppRoutingModule导入到app.module.ts中?

@NgModule({ imports: [ BrowserModule, FormsModule, AppRoutingModule ],

© www.soinside.com 2019 - 2024. All rights reserved.