【发布时间】:2020-05-26 06:56:56
【问题描述】:
我正在使用 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 {}
app-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。这与我的组件有关吗?
【问题讨论】:
-
您的 HTML 模板中是否有
<router-outlet></router-outlet>标签? -
是的,在我的 app.component.html 中。
-
为什么你在 AppModule 中同时拥有
RouterModule.forRoot([,但又导入了一个同样拥有RouterModule.forRoot([的路由模块?这也无济于事,因为它们在两者中都有一些相同的路线。 -
看起来我正在测试这两种方案,看看其中一种是否能胜过另一种。一个比另一个更好吗?
-
最好使用
AppRoutingModule。在你的 appModule 中删除RouterModule.forRoot(...)
标签: angular routing navigation components angular8