通过Bootstrap Nav栏路由不同的角度组件

发布于 2025-01-22 16:18:43 字数 2383 浏览 1 评论 0原文

我正在尝试使用NAV栏在Angular中导航到我的不同组件。

首先,我为此NAV栏创建了一个名为Header的组件,并为NAV栏编写了HTML代码:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">NBA Statistics</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
  

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav mr-auto">
          <li *ngFor = "let items of menuItems" class="nav-item">
            <a class="nav-link" href="{{items.linkURL}}">{{items.linkName}} <span class="sr-only">(current)</span></a>
          </li>
      </ul>
    </div>
  </nav>

在我的Header.component.ts中,我在上面的HTML代码中包括了一个不同页面的数组:


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

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

  menuItems = [

    {linkId: 1, linkName: 'Search by Player', linkURL: 'home'},
    {linkId: 2, linkName: 'Lab 5', linkURL: 'lab5'},
    {linkId: 3, linkName: 'Lab 6', linkURL: 'lab6'},
    {linkId: 4, linkName: 'Graphs', linkURL: 'graphs'}

  ]
  constructor() { }

  ngOnInit(): void {
  }

}

然后在我的HTML代码中 : app-routing.module.ts,我创建了路由路径:


import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { GraphsComponent } from './graphs/graphs.component';
import { Lab5Component } from './lab5/lab5.component';
import { MongodbComponent } from './mongodb/mongodb.component';
import { NbaStatsComponent } from './nba-stats/nba-stats.component';

const routes: Routes = [

  {path: 'home', component: NbaStatsComponent},
  {path: 'lab5', component: Lab5Component},
  {path: 'lab6', component: MongodbComponent},
  {path:'graphs', component: GraphsComponent}

];

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

导航栏出现正确,但是当我单击不同的链接以导航到该页面时,它说“无法获得 /回家”。我是否错过了路由中的其他任何步骤?

I am trying to navigate to my different components in angular using a nav bar.

First, I created a component for this nav bar called header and wrote the HTML code for the nav bar:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">NBA Statistics</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
  

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav mr-auto">
          <li *ngFor = "let items of menuItems" class="nav-item">
            <a class="nav-link" href="{{items.linkURL}}">{{items.linkName}} <span class="sr-only">(current)</span></a>
          </li>
      </ul>
    </div>
  </nav>

In my header.component.ts, I included an array for the different pages which I referenced in my HTML code above:


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

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

  menuItems = [

    {linkId: 1, linkName: 'Search by Player', linkURL: 'home'},
    {linkId: 2, linkName: 'Lab 5', linkURL: 'lab5'},
    {linkId: 3, linkName: 'Lab 6', linkURL: 'lab6'},
    {linkId: 4, linkName: 'Graphs', linkURL: 'graphs'}

  ]
  constructor() { }

  ngOnInit(): void {
  }

}

Then in my app-routing.module.ts, I created routing paths:


import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { GraphsComponent } from './graphs/graphs.component';
import { Lab5Component } from './lab5/lab5.component';
import { MongodbComponent } from './mongodb/mongodb.component';
import { NbaStatsComponent } from './nba-stats/nba-stats.component';

const routes: Routes = [

  {path: 'home', component: NbaStatsComponent},
  {path: 'lab5', component: Lab5Component},
  {path: 'lab6', component: MongodbComponent},
  {path:'graphs', component: GraphsComponent}

];

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

The nav bar appears properly but when I click on the different links to navigate to that page it says "Cannot GET /home". Am I missing anyother steps in the routing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

非要怀念 2025-01-29 16:18:43

您不会错过一步,但是您应该使用 routerlink routerlink 指令< /a>而不是 href属性,您的路线应从前向斜杠开始。

使用HREF,您的浏览器将尝试获取不存在的EG /homelocalhost:4200/home存在,因为Angular执行路由对于您,您的项目根部没有静态 home.html 文件)。

使用routerlink/home,您的浏览器始于localhost:4200 angular 更改&lt; router-outlet&gt; to home路径确定的组件。 这是更多的路由教程

而不是:

<a href="{{items.linkURL}}">{{items.linkName}}</a>
menuItems = [
    {linkId: 1, linkName: 'Search by Player', linkURL: 'home'},
    {linkId: 2, linkName: 'Lab 5', linkURL: 'lab5'},
    {linkId: 3, linkName: 'Lab 6', linkURL: 'lab6'},
    {linkId: 4, linkName: 'Graphs', linkURL: 'graphs'}
];

您想要:

<a [routerLink]="[items.linkURL]">{{items.linkName}}</a>
menuItems = [
    {linkId: 1, linkName: 'Search by Player', linkURL: '/home'},
    {linkId: 2, linkName: 'Lab 5', linkURL: '/lab5'},
    {linkId: 3, linkName: 'Lab 6', linkURL: '/lab6'},
    {linkId: 4, linkName: 'Graphs', linkURL: '/graphs'}
];

You're not missing a step, but you should be using the routerLink directive instead of the href attribute and your routes should start with a forward slash.

Using href, your browser will try to fetch e.g. /home which doesn't exist (localhost:4200/home exists because Angular performs the routing for you. There is no static home.html file in the root of your project).

Using routerLink with /home, your browser stays on localhost:4200 and Angular changes the content of the <router-outlet> to the component identified by the home path. Here's the routing tutorial for more.

Instead of:

<a href="{{items.linkURL}}">{{items.linkName}}</a>
menuItems = [
    {linkId: 1, linkName: 'Search by Player', linkURL: 'home'},
    {linkId: 2, linkName: 'Lab 5', linkURL: 'lab5'},
    {linkId: 3, linkName: 'Lab 6', linkURL: 'lab6'},
    {linkId: 4, linkName: 'Graphs', linkURL: 'graphs'}
];

You want:

<a [routerLink]="[items.linkURL]">{{items.linkName}}</a>
menuItems = [
    {linkId: 1, linkName: 'Search by Player', linkURL: '/home'},
    {linkId: 2, linkName: 'Lab 5', linkURL: '/lab5'},
    {linkId: 3, linkName: 'Lab 6', linkURL: '/lab6'},
    {linkId: 4, linkName: 'Graphs', linkURL: '/graphs'}
];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文