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

相关推荐
kymjs张涛4 分钟前
零一开源|前沿技术周报 #7
android·前端·ios
爱编程的喵9 分钟前
React入门实战:从静态渲染到动态状态管理
前端·javascript
Tttian62221 分钟前
npm init vue@latestnpm error code ETIMEDOUT
前端·vue.js·npm
患得患失94928 分钟前
【前端】【组件库开发】【原理】【无框架开发】现代网页弹窗开发指南:从基础到优化
前端
运维咖啡吧37 分钟前
给朋友们分享个好消息 7天时间23.5k
前端·程序员·ai编程
王中阳Go39 分钟前
面试完第一反应是想笑
后端·go
元气小嘉1 小时前
前端技术小结
开发语言·前端·javascript·vue.js·人工智能
Livingbody1 小时前
10分钟实现基于Ubuntu25.04本地推理ERNIE模型
后端
神仙别闹1 小时前
基于ASP.NET+SQL Server实现(Web)企业进销存管理系统
前端·后端·asp.net
5171 小时前
django中如何使用Django REST Framework
后端·python·django