javascript - 即使路径正确,routerLink 也不显示正确的 HTML
问题描述
这是我的应用模块文件
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { FlashMessagesModule } from 'angular2-flash-messages';
import { FormsModule } from '@angular/forms';
import { environment } from '../environments/environment';
import { AngularFireModule } from '@angular/fire';
import { AngularFirestoreModule } from '@angular/fire/firestore';
import { AngularFireAuthModule } from '@angular/fire/auth';
import { AppComponent } from './app.component';
import { LoginComponent } from './components/login/login.component';
import { NavbarComponent } from './components/navbar/navbar.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';
import { AddFoodComponent } from './components/add-food/add-food.component';
import { EditClientComponent } from './components/edit-client/edit-client.component';
import { ClientInfoComponent } from './components/client-info/client-info.component';
import { ClientComponent } from './components/client/client.component';
import { RegisterComponent } from './components/register/register.component';
import { SidebarComponent } from './components/sidebar/sidebar.component';
@NgModule({
declarations: [
AppComponent,
LoginComponent,
NavbarComponent,
DashboardComponent,
EditClientComponent,
ClientInfoComponent,
RegisterComponent,
ClientComponent,
AddFoodComponent,
SidebarComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
AngularFireModule.initializeApp(
environment.firebaseConfig,
'calorie-counter'
),
AngularFirestoreModule,
AngularFireAuthModule,
FlashMessagesModule.forRoot(),
FormsModule,
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
这是我的 app-routing.module 文件
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ClientInfoComponent } from './components/client-info/client-info.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';
import { EditClientComponent } from './components/edit-client/edit-client.component';
import { LoginComponent } from './components/login/login.component';
import { NavbarComponent } from './components/navbar/navbar.component';
import { ClientComponent } from './components/client/client.component';
import { RegisterComponent } from './components/register/register.component';
import { AddFoodComponent } from './components/add-food/add-food.component';
const routes: Routes = [
{ path: '', component: DashboardComponent },
{ path: 'client', component: ClientComponent },
{ path: 'client/edit/:id', component: EditClientComponent },
{ path: 'login', component: LoginComponent },
{ path: 'navbar', component: NavbarComponent },
{ path: 'client/:id', component: ClientInfoComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'client/add', component: AddFoodComponent },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {}
这是我尝试显示但未显示的组件 html,而是显示另一个组件的 html
这是我在运行 localhost:4200/client/add 时要生成的 html,代码如下:
文件:add-food-component.html
<div class="row">
<div class="col-md-6">
<a routerLink="/" class="btn btn-link">
<i class="fa fa-arrow-circle-o-left"></i> Back To Dashboard
</a>
</div>
<div class="col-md-6"></div>
</div>
<div class="card">
<div class="card-header">
Add Client
</div>
<div class="card-body">
<form #foodForm="ngForm" (ngSubmit)="onSubmit(foodForm)">
<div class="form-group">
<label for="foodName">First Name</label>
<input
type="text"
class="form-control"
name="foodName"
#fName="ngModel"
[ngClass]="{ 'is-invalid': fName.errors && fName.touched }"
[(ngModel)]="client.food"
minlength="2"
required
/>
<div [hidden]="!fName.errors?.required" class="invalid-feedback">
Food required
</div>
<div [hidden]="!fName.errors?.minlength" class="invalid-feedback">
Must be at least 2 characters
</div>
</div>
<div class="form-group">
<label for="calories">Calories</label>
<input
type="text"
class="form-control"
name="calories"
#calories="ngModel"
[ngClass]="{ 'is-invalid': calories.errors && calories.touched }"
[(ngModel)]="client.calories"
minlength="2"
required
/>
<div [hidden]="!calories.errors?.required" class="invalid-feedback">
Last name required
</div>
<div [hidden]="!calories.errors?.minlength" class="invalid-feedback">
Must be at least 2 characters
</div>
</div>
<input type="submit" value="Submit" class="btn btn-primary btn-block" />
</form>
</div>
</div>
所以由于某种原因,当我去路线 localhost:4200/client/add 它的显示
“client-info works”,它是 client-info.component.html 的 html 组件
我的路线是
<a routerLink="/client/add" class="btn btn-success btn-block">
<i class="fa fa-plus"></i> Add
</a>
任何帮助表示赞赏!我已经坚持了很长时间。
解决方案
尝试重新排序您的路线,以便 path:'client/add' 在您的路线列表中的 path:'client/:id' 之前。
Angular 路由器按顺序处理路由并获取第一个匹配项。'client/add' 可以解释为 'client/:id' 其中 ':id' 映射到 'add'。因此,它将它作为列表中的第一个匹配项。
路线顺序
路由的顺序很重要,因为路由器在匹配路由时使用先匹配获胜策略,因此更具体的路由应该放在不太具体的路由之上。首先列出具有静态路径的路由,然后是与默认路由匹配的空路径路由。通配符路由位于最后,因为它匹配每个 URL,并且路由器仅在没有其他路由首先匹配时才选择它。
推荐阅读
- azure-devops - 使用 yaml 自动取消对环境的构建/部署(因为不支持手动仅触发预部署条件)
- javascript - ^ 1 在javascript中是什么意思?
- ansible - Oneliner 断言列表的所有元素
- julia - Searchligh.jl 支持哪些数据类型?
- jquery - 如何在分页版本:4时设置jquery select2的选定值?
- api - 为访客和授权页面编写端点的最佳方法是什么
- reactjs - React useCallback 在第一次更改时不更新状态 onChange
- javascript - 构建和上传项目时在服务器中找不到文件
- android - AAPT:错误:找不到资源可绘制/edittext_search_donation_request
- groovy - 我有身份证清单。现在我想将它们存储在数组中以匹配 id