When I call a component in Angular it doesn't appears, and the home component still appears even when the endpoint changes

huangapple go评论72阅读模式
英文:

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 { }

When I call a component in Angular it doesn't appears, and the home component still appears even when the endpoint changes
When I call a component in Angular it doesn't appears, and the home component still appears even when the endpoint changes

英文:

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)

When I call a component in Angular it doesn't appears, and the home component still appears even when the endpoint changes
When I call a component in Angular it doesn't appears, and the home component still appears even when the endpoint changes

Student component

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import { Component, OnInit } from &#39;@angular/core&#39;;

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

  constructor() { }

  ngOnInit(): void {
  }

}

<!-- language: lang-html -->

&lt;p&gt;student works!&lt;/p&gt;

<!-- end snippet -->

app.routing.module.ts

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

const routes: Routes = [{ path: &#39;student&#39;, 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 -->

&lt;h1&gt;Grade Management&lt;/h1&gt;

<!-- end snippet -->

app.component.ts

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import { Component } from &#39;@angular/core&#39;;

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

<!-- end snippet -->

app.module.ts

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import { NgModule } from &#39;@angular/core&#39;;
import { BrowserModule } from &#39;@angular/platform-browser&#39;;

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

@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 中添加以下内容来解决了问题。

&lt;router-outlet&gt;&lt;/router-outlet&gt;
英文:

Thank you all, I've fixed the problem by adding the following in app.component.html.

&lt;router-outlet&gt;&lt;/router-outlet&gt;

huangapple
  • 本文由 发表于 2023年4月19日 14:40:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定