英文:
When I call a component in Angular it doesn't appears, and the home component still appears even when the endpoint changes
问题
I've translated the provided content:
我创建了一个名为 "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 { }
英文:
I've created a "student" component and when I called it it doesn't appears in the endpoint where it should be located! instead, the home component still appears even in the student endpoint (/student)
Student component
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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 {
}
}
<!-- language: lang-html -->
<p>student works!</p>
<!-- end snippet -->
app.routing.module.ts
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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 {}
<!-- end snippet -->
app.component.html
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<h1>Grade Management</h1>
<!-- end snippet -->
app.component.ts
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'GradeManagement_FrontEnd';
}
<!-- end snippet -->
app.module.ts
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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 { }
<!-- end snippet -->
答案1
得分: 0
请提供您的app.component.html的内容。
实际上,在启动应用程序时加载的是index.html文件,它应该包含"app-root"标签(项目的根文件)。实际上,您的app.component.html应该包含"router-outlet"标签以允许路由。
英文:
Could you please provide the content of your app.component.html
Indeed, when you start your application it's the index.html which is loaded and it should contains "app-root" tag (file root of project). In practice your app.component.html should contain "router-outlet" tag to permit the routing
答案2
得分: 0
谢谢大家,我已经通过在 app.component.html
中添加以下内容来解决了问题。
<router-outlet></router-outlet>
英文:
Thank you all, I've fixed the problem by adding the following in app.component.html
.
<router-outlet></router-outlet>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论