Angular 17 Vite 带来了新的开发体验

一、简介

txt 复制代码
 _________________________
< Hi~ 开始体验 Angular 17 >
 -------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Angular 17 带来了很多新的内容:

  • 使用 esm 输出
  • 使用 esbuild + vite 构建
  • 集成 SSR 与预渲染功能

二、Angular 脚手架

sh 复制代码
npm i -g @angular/cli 

安装之后得到了 ng 命令

  • 初始化
sh 复制代码
ng new demo1
  • 目录结构

启动 vite 文件在 .angular 文件中得以体现。

sh 复制代码
/root/demo1
├── README.md
├── angular.json # angular 配置
├── node_modules
├── package-lock.json
├── package.json
├── src # 源代码位置
├── tsconfig.app.json
├── tsconfig.json
└── tsconfig.spec.json
  • 启动服务
sh 复制代码
npm run start
  • 预览
  • vite 在浏览器中的变化 (websocket)

三、生成一个路由组件

sh 复制代码
ng g About

创建如下四个文件:

sh 复制代码
CREATE src/app/about/about.component.css (0 bytes)
CREATE src/app/about/about.component.html (20 bytes)
CREATE src/app/about/about.component.spec.ts (589 bytes)
CREATE src/app/about/about.component.ts (290 bytes)

四、配置路由

ts 复制代码
import { Routes } from '@angular/router';
import { AboutComponent } from './about/about.component';

export const routes: Routes = [
  { path: 'about', component: AboutComponent },
];

本质就是添加一个 JS 对象,是 path 与 component。

五、a 标签导航到新的路由

html 复制代码
<ul>
  <li><a routerLink="/about">About</a></li>
</ul>

六、编程式导航

html 复制代码
<div (click)="navigateToAbout()">go about</div>

div 上绑定导航事件,在 class 中定义路由导航

ts 复制代码
import { Router } from '@angular/router';

//...
export class AppComponent {
  constructor(private router: Router) { }
  navigateToAbout() {
    this.router.navigate(['/about']);
  }
}

在构造函数中注入 router 对象,然后 this 中就包含了 router 对象。

七、内置 rxjs

ts 复制代码
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Observable, interval, take } from 'rxjs';

@Component({
  selector: 'app-about',
  standalone: true,
  imports: [CommonModule],
  templateUrl: './about.component.html',
  styleUrl: './about.component.css'
})
export class AboutComponent {
  count: number = 0;

  constructor() {}

  ngOnInit() {
    let that = this
     // 创建一个每秒发出一个值的 Observable
    interval(1000).pipe(
      // 如果你想在一定数量的值之后停止,可以使用 `take` 操作符
      take(10) // 例如,这里是每隔一秒发出一个值,共发出10个值
    ).subscribe({
      next() {
        that.count += 1
      }
    })
  }
}

定义一个状态 count, 然后使用 rxjs 定时器 interval 中,不断改变 html 中的 count 的状态。

八、小结

文本主要体验 Angular 17 带来的新的编程体验,从脚手架,到路由配置,到rxjs处理状态,体验 Vite 在 Angular 17 中的新的编程体验。当然 在 Angular 17 中也带来了 SSR 等特性,对 Angular 感兴趣可以深入探索。

相关推荐
芝芝葡萄14 分钟前
VsCode中使用Codex
前端·ide·vscode·编辑器·ai编程
wangmengxxw16 分钟前
SpringAI-mcp-入门案例
java·服务器·前端·大模型·springai·mcp
觉醒大王19 分钟前
简单说说参考文献引用
java·前端·数据库·学习·自然语言处理·学习方法·迁移学习
weixin_4492900121 分钟前
EverMemOS 访问外部(deepinfra)API接口
java·服务器·前端
鱼毓屿御26 分钟前
Tailwind CSS配置进阶
前端·css
Mr Xu_30 分钟前
Git常用指令
前端·git
michael_ouyang35 分钟前
IM 会话同步企业级方案选型
前端·websocket·electron·node.js
betazhou37 分钟前
借用Deepseek写一个定期清理备份文件的ps脚本
开发语言·前端·javascript·ps·deepseek·清理备份文件
我爱娃哈哈39 分钟前
SpringBoot 实现 RSA+AES 自动接口解密
java·spring boot·后端
崎岖Qiu40 分钟前
SpringBoot:基于注解 @PostConstruct 和 ApplicationRunner 进行初始化的区别
java·spring boot·后端·spring·javaee