当前位置: 首页 > news >正文

[Angular] 笔记 18:Angular Router

Angular Router 视频

chatgpt:
Angular 具有内置的大量工具、功能和库,功能强大且经过良好设计,如组件化架构、依赖注入、模块化系统、路由和HTTP客户端等。这些功能可以直接用于项目中,无需额外的设置或第三方库。这简化了开发流程,因为不必从头编写或集成许多常见的功能,而是可以利用Angular提供的工具快速启动和构建应用程序。

也就是说,Angular 是一种自带电池(Batteries Included)的框架,web 开发所需要的一切应用尽有,Router 是其中之一。

当创建Angular app时,使用命令 ng new <app-name>, Angular 接着会问要不要 Routing 功能 ? 选择 yes, 生成的 app 就会带有 routing 模块:

在这里插入图片描述

1. 注册 routes

app-routing.module.ts:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';// 这里注册两个 route:homepage route 和 通配符 route
const routes: Routes = [{ path: '', component: HomeComponent, pathMatch: 'full' },{ path: '**', component: NotfoundComponent },
];@NgModule({imports: [RouterModule.forRoot(routes)],exports: [RouterModule],
})
export class AppRoutingModule {}

2. 生成与所注册的 routes 对应的两个组件

在这里插入图片描述

一个组件名称为 notfound, 另一个为 home.

奇怪的是上述视频中的方法不起作用,没法生成组件,使用 ng-cli 命令:

PS D:\Angular\my-app> ng generate component home --module=app.module.ts    
CREATE src/app/home/home.component.html (19 bytes)
CREATE src/app/home/home.component.spec.ts (585 bytes)
CREATE src/app/home/home.component.ts (267 bytes)
CREATE src/app/home/home.component.css (0 bytes)
UPDATE src/app/app.module.ts (727 bytes)
PS D:\Angular\my-app> ng generate component notfound --module=app.module.ts
CREATE src/app/notfound/notfound.component.html (23 bytes)
CREATE src/app/notfound/notfound.component.spec.ts (613 bytes)
CREATE src/app/notfound/notfound.component.ts (283 bytes)
CREATE src/app/notfound/notfound.component.css (0 bytes)
UPDATE src/app/app.module.ts (813 bytes)
PS D:\Angular\Angular Tutorial For Beginners 2022\my-app> 

在这里插入图片描述

3. 核对 index.html 内容

此文件中必须有:<base href="/"> 以及 <app-root></app-root>, 缺一不可,否则 routing 部分就不起作用。

<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>MyApp</title><base href="/"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body><app-root></app-root>
</body>
</html>

4. 连结 router 链接

app.component.html

<router-outlet></router-outlet>
<div><a routerLink="/"></a>
</div>

缺少了 router-outlet, routing 也会不起作用

5. Navbar interface

app.component.ts:

import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';// navbar interface
interface Nav {link: string;name: string;exact: boolean;
}@Component({selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.css'],
})
export class AppComponent {constructor() {}
}

6. 修改 app.component.html

将其中的 <a> 改成使用 for loop:

<router-outlet></router-outlet>
<div><a*ngFor="let item of nav"[routerLink]="item.link"routerLinkActive="active"[routerLinkActiveOptions]="{ exact: item.exact }">{{ item.name }}</a>
</div>

routerLinkActive 用于设置当前链接是否为 active,即所在页面是否与当前链接对应。

同时设置 app.component.css,设置 active 链接的背景色为红色:

.cool-bool {background: #0094ff;
}.active {background-color: red;
}

7. 运行 ng serve:

在这里插入图片描述

http://www.lryc.cn/news/269568.html

相关文章:

  • 微服务全链路灰度方案介绍
  • 低代码开发OA系统 低代码平台如何搭建OA办公系统
  • 构建Python的Windows整合包教程
  • 《整机柜服务器通用规范》由OCTC正式发布!浪潮信息牵头编制
  • Linux:修改和删除已有变量
  • 【23.12.29期--Spring篇】Spring的 IOC 介绍
  • 【Python排序算法系列】—— 选择排序
  • 会议室占用时间段 - 华为OD统一考试
  • 计算机网络复习5
  • React Hooks 面试题 | 05.精选React Hooks面试题
  • 2024收入最高的编程语言
  • Android笔记(二十三):Paging3分页加载库结合Compose的实现分层数据源访问
  • Python实现马赛克图片处理
  • 你能描述下你对vue生命周期的理解?在created和mounted这两个生命周期中请求数据有什么区别呢?
  • 【经典算法】有趣的算法之---蚁群算法梳理
  • 第八届视觉、图像与信号处理国际会议(ICVISP 2024) | Ei, Scopus双检索
  • 《HelloGitHub》第 93 期
  • JAVA B/S架构智慧工地源码,PC后台管理端、APP移动端
  • 【adb】--- win10 配置 adb环境 超详细 (持续更新中)
  • SQL注入安全漏洞详解
  • 数据结构与算法教程,数据结构C语言版教程!(第一部分、数据结构快速入门,数据结构基础详解)四
  • mac安装k8s环境
  • HarmonyOS4.0系列——04、@Styles、@Extend、@Extend事件以及多态样式stateStyles
  • C++项目之酒店客房管理系统架构——设计模式应用场景详解(下)
  • RabbitMQ消息存储JSON格式反序列化
  • Java解决统计有序矩阵中的负数问题
  • 【算法与数据结构】435、LeetCode无重叠区间
  • 【开题报告】基于SpringBoot的茶文化宣传网站设计与实现
  • 用通俗易懂的方式讲解大模型:基于 Langchain 和 ChatChat 部署本地知识库问答系统
  • YOLO训练results.csv文件可视化(原模型与改进模型对比可视化)