【发布时间】:2020-06-13 11:36:08
【问题描述】:
Angular 新手,非常感谢您的耐心等待。
我想达到什么目的?
- 我正在尝试在我的 auth 模块中使用该组件(即登录 component) 作为路由 myapp.com/login 的基础组件
- 假设在 **app.component.html 中嵌入 可以正常工作,那么当我将其放入 app-routing.module.ts 时应该也是如此
app-routing.module.ts 的代码
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: '/login', component: LoginComponent}, //Cannot find name 'LoginComponent'.ts(2304)
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
我的组件结构
- 我有一个身份验证模块,其中包含登录和注册组件
- 以及基本的 ng-cli 生成的应用程序结构。
authmodule 的代码 auth.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RegisterComponent } from './register/register.component';
import { LoginComponent } from './login/login.component';
@NgModule({
declarations: [RegisterComponent, LoginComponent],
exports:[RegisterComponent,LoginComponent],
imports: [
CommonModule
]
})
export class AuthModule { }
app.module.ts 的代码
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import {AppCommonsModule} from './app-commons/app-commons.module';
import {AuthModule} from './auth/auth.module'
import { HomeComponent } from './home/home.component'
@NgModule({
declarations: [
AppComponent,
HomeComponent
],
imports: [
BrowserModule,
AppRoutingModule,
AppCommonsModule,
AuthModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
【问题讨论】:
-
您需要在
app-routing.module.ts的顶部导入LoginComponent,就像HomeComponent.. -
当然,但是在 app.module.ts 中导入这个模块有什么意义呢?
-
app-routing.module.ts是路由的“控制器”。其他模块是会议场所的“控制器”,让所有东西都能看到对方。您在app-routing.module.ts中看到,您没有将LoginComponent放入declarations: []。为了使用组件,它必须位于declarations: []。
标签: javascript angular typescript angular-routing