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 感兴趣可以深入探索。

相关推荐
Bs_MoneyMagnet2 小时前
基于springboot+vue的滑雪场票务与装备租赁系统的设计与实现 源码+文档
java·vue.js·spring boot·后端·spring·毕业设计·计算机毕业设计
AlienZHOU6 小时前
AI Coding 时代下,我的技术面试实践分享
前端·后端·面试
狗头大军之江苏分军8 小时前
《潮水漫过十七岁》开学了
后端
苏三说技术9 小时前
如何看待GPT-6在UP主众测中碾压夺冠?它是现在最强大模型吗?
后端
mldong9 小时前
一份 JSON,一条能跑的审批流:把报销流程送上工作流引擎
后端·架构
wno7049 小时前
Spring Boot WebFlux增删改查
java·spring boot·后端
Captaincc9 小时前
AI用量v0.1.11更新发布 新增 jusage doctor 诊断指令 托盘展示token 和余额 新增 AutoClaw 支持
前端·后端·vibecoding
aramae10 小时前
模拟实现strlen()函数 (C语言)
c语言·开发语言·后端
计算机魔术师11 小时前
德国Wiki被黑后两周,OpenAI终于把模型失控的账本摊开了
前端
kyriewen11 小时前
我让 AI 当面试官面了我一轮:第 3 个追问我就卡住了(附 10 道追问清单)
前端·面试·ai编程