路由和导航:无法访问网页上的任何其他路由

Routing & Navigation: Cannot access any other routes on web page

我正在使用 Angular 8,但我的页面上出现了路由问题。我已经生成了我需要的组件,并声明了它们并使用必要的路径导入它们,以便在网页上导航。

app.module.ts:

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

import { AngularFireModule } from "@angular/fire";
import { AngularFireAuthModule } from "@angular/fire/auth";
import { AngularFirestoreModule } from "@angular/fire/firestore";

import { HomeComponent } from "./home/home.component";
import { LoginComponent } from "./login/login.component";
import { SignupComponent } from "./signup/signup.component";

import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
import { environment } from "src/environments/environment";

@NgModule({
  declarations: [AppComponent, HomeComponent, LoginComponent, SignupComponent],
  imports: [
    BrowserModule,
    FormsModule,
    RouterModule.forRoot([
      { path: "", component: HomeComponent },
      { path: "login", component: LoginComponent },
      { path: "signup", component: SignupComponent }
    ]),
    AppRoutingModule,
    AngularFireModule.initializeApp(environment.firebase),
    AngularFireAuthModule,
    AngularFirestoreModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

应用-routing.module.ts:

import { NgModule } from "@angular/core";
import { Routes, RouterModule } from "@angular/router";
import { HomeComponent } from "./home/home.component";
import { LoginComponent } from "./login/login.component";
import { SignupComponent } from "./signup/signup.component";

const routes: Routes = [
  {
    path: "login",
    component: LoginComponent
  },

  {
    path: "home",
    component: HomeComponent
  },

  {
    path: "signup",
    component: SignupComponent
  },
  {
    path: "",
    redirectTo: "/home",
    pathMatch: "full"
  }
];

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

因为我已经将所有必要的组件添加到应用程序模块中,所以当我更改 URL 并添加 /login 或 /signup 时它不应该工作吗? 另外请注意,我无法在组件中看到我的 html 代码。它在我的网站上唯一可见的地方是 index.html。这与我的组件有关吗?

您的问题是您在其他路线之前声明了空路线。 Angular 将始终将您转到它匹配的第一个 url。在您的情况下,{ path: "", component: HomeComponent } 是第一个匹配项,无论您尝试访问的是 url。

提示:尝试像您一样始终使用 RoutingModule,但不要导入 RouterModule.forRoot(...)。只需删除它就可以了。