迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > Angular >

在 Angular 中创建导航栏

作者:迹忆客 最近更新:2023/04/14 浏览次数:

本教程将介绍如何在 Angular 中创建导航栏并将其用于 Angular 应用程序中的导航。


Angular 中的导航栏

导航栏是任何 Web 应用程序的重要组成部分之一。即使在构建仅包含一个页面但不同部分的单页应用程序 (SPA) 时,我们仍然使用导航栏从一个部分导航到另一个部分。

导航栏使用户可以轻松地在你的 Web 应用程序中找到他们正在寻找的内容。

导航有很多方法可以实现从简单到复杂的路由。Angular 提供了一个单独的模块,可以帮助我们在 Web 应用程序中创建导航栏并将其用于导航。

如果我们想以编程方式将用户从一个页面导航到另一个页面,我们可以使用路由 navigate() 方法,这意味着我们在不使用确切链接的情况下导航组件。我们使用组件名称来代替。


在 Angular 中创建导航栏

让我们来看一个示例,在该示例中,我们将创建一个导航栏,并使用 navigate() 在不同的组件中导航。因此,让我们使用以下命令创建一个新应用程序。

# angular
ng new my-app

创建我们的新应用程序后,我们将使用此命令转到我们的应用程序目录。

# angular
cd my-app

现在,让我们运行我们的应用程序以确保正确安装所有依赖项。

# angular
ng serve --open

在 Angular 中生成导航栏组件

现在,让我们生成导航栏组件。首先,我们将生成我们的 index 组件,它将充当我们应用程序的 home 组件。

# angular
ng generate component index

然后,我们将生成 about us 组件。

# angular
ng generate component aboutUs

最后,我们将使用以下命令生成我们的 products 组件。

# angular
ng generate component products

我们将在带有 3 个文件的单独文件夹中包含 3 个组件,你可以在下面看到。

输出:

为导航栏创建组件后的文件夹结构

为组件创建视图

现在,我们将为我们的组件创建视图。首先,我们将从 about 文件夹打开 aboutUs.component.html 并添加以下代码。

# angular
<div class="container" >
  <h1> This is about Us Component </h1>
  <h3> This is the content of About Us </h3>
</div>

接下来,我们将打开 home 文件夹中的 index.component.html 文件并添加以下代码。

# angular
<div class="container">
 <h1> This is index component </h1>
  <h3> This is the content of Index </h3>
</div>

我们将打开 services 文件夹中的 products.component.html 文件并添加以下代码。

# angular
<div class="container">
 <h1> This is products component </h1>
  <h3> This is the content of Products </h3>
</div>

定义组件路由

一旦我们创建了我们的组件和视图,我们将在 app-routing.module.ts 中定义我们的路由。

我们将导入 ngModule。我们还将从路由中导入 RoutesRouterModule,最后但并非最不重要的是,我们刚刚创建的组件。

# angular
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AboutUsComponent}  from './aboutUs/aboutUs.component';
import { IndexComponent} from './index/index.component';
import { ProductsComponent } from './products/products.component';

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

导入后,我们将定义我们组件的路由,如下所示。

# angular
const routes: Routes = [

    { path: 'aboutUs', component: AboutUsComponent },
    { path: 'index', component: IndexComponent},
    { path: 'products', component: ProductsComponent },
 ];

创建导航菜单

然后我们将在 app.component.html 中创建导航菜单。每个链接都将使用 (click) 方法调用一个函数。

我们将使用 router-outlet 显示组件数据,如下所示。

# angular
<ul class="nav navbar-nav">
  <li>
    <a (click)="goToIndex()">Home</a>
  </li>
  <li>
    <a (click)="goToAboutUs()">About Us</a>
  </li>
  <li>
    <a (click)="goToProducts()">Products</a>
  </li>
</ul>

<router-outlet> </router-outlet>

创建在组件之间导航的函数

接下来,我们将创建函数 goToIndex()goToAboutUs()goToProducts()。我们将打开 app.component.ts 并导入 Router

使用 router.navigate(),我们将创建这些函数来在组件之间导航。

# angular
import { Component } from '@angular/core';
import { Router } from '@angular/router';


@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  constructor(private router: Router) {
  }

  goToIndex() {
    this.router.navigate(['/', 'index']);
  }
  goToAboutUs() {
    this.router.navigate(['/', 'aboutUs']);
  }
  goToProducts() {
    this.router.navigate(['/', 'products']);
  }
}

输出:

Angular 中使用 Navigate 方法的导航栏工作示例

从示例中可以看出,我们可以轻松地在 Angular 应用程序中创建导航栏,并使用 navigate() 在组件之间导航并定义路由。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便