typescript 当我在Angular中调用一个组件时,它不会出现,即使端点发生变化,home组件仍然会出现

w80xi6nr  于 2023-04-22  发布在  TypeScript
关注(0)|答案(2)|浏览(70)

我创建了一个“student”组件,当我调用它时,它没有出现在它应该位于的端点中!相反,home组件甚至仍然出现在student端点(/student)中。

学生组件

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-student',
  templateUrl: './student.component.html',
  styleUrls: ['./student.component.css']
})
export class StudentComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

}
<p>student works!</p>

app.routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { StudentComponent } from './components/student/student.component';

const routes: Routes = [{ path: 'student', component: StudentComponent },];

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

app.component.html

<h1>Grade Management</h1>

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'GradeManagement_FrontEnd';
}

app.module.ts

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

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { StudentComponent } from './components/student/student.component';

@NgModule({
  declarations: [
    AppComponent,
    StudentComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
nbnkbykc

nbnkbykc1#

请提供您的app.component.html的内容
实际上,当您启动应用程序时,将加载index.html,并且它应该包含“app-root”标记(项目的文件根)。

toe95027

toe950272#

谢谢大家,我已经通过将<router-outlet></router-outlet>添加到

app.component.html

相关问题