typescript 由于某种原因,Angular 路由器链接不工作

wko9yo5t  于 2023-01-27  发布在  TypeScript
关注(0)|答案(1)|浏览(101)

我不知道为什么这个路由器链接不工作
我只是想让这个路由器导航到搜索模块。好有趣的一点是,页面url是localhost 4200/technology,但仍然停留在主页上,好像没有搜索组件。我真的不知道该怎么办

``` homepage router ```
<a class="link-header" routerLink="search">See Recipe</a>

my app module file
import { RouterModule } from '@angular/router';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { MealsComponent } from './meals/meals.component';
import { HttpClientModule } from '@angular/common/http';
import { SearchMealComponent } from './search-meal/search-meal.component';
import { MealAppComponent } from './meal-app/meal-app.component';
import { HomeComponent } from './home/home.component';
import { ReactiveFormsModule } from '@angular/forms';

@NgModule({
declarations: [
AppComponent,
MealsComponent,
SearchMealComponent,
MealAppComponent,
HomeComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
ReactiveFormsModule
],
providers: [RouterModule],
bootstrap: [AppComponent]
})
export class AppModule { }


``` app-routing-module paths ```
 const routes: Routes = [
  { path:'search', component:SearchMealComponent }
];
u5i3ibmn

u5i3ibmn1#

问题似乎是app-routing.modules.ts文件配置不正确
homepage router

<a class="link-header" routerLink="search">See Recipe</a>

my app module file

import { RouterModule } from '@angular/router';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { MealsComponent } from './meals/meals.component';
import { HttpClientModule } from '@angular/common/http';
import { SearchMealComponent } from './search-meal/search-meal.component';
import { MealAppComponent } from './meal-app/meal-app.component';
import { HomeComponent } from './home/home.component';
import { ReactiveFormsModule } from '@angular/forms';

@NgModule({
  declarations: [
    AppComponent,
    MealsComponent,
    SearchMealComponent,
    MealAppComponent,
    HomeComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    ReactiveFormsModule
  ],
  providers: [RouterModule],
  bootstrap: [AppComponent]
})
 export class AppModule { }

app-routing-module paths

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { SearchMealComponent } from './search-meal/search-meal.component';

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

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

export class AppRoutingModule { }

这些问题通常发生在您创建新文件时不使用ng generate的情况下,您将面临排除资产或依赖项的风险。
希望能有所帮助。

相关问题