Angular 尝鲜

什么是 Angular ?

官网描述:Angular 是一个应用设计框架开发平台,旨在创建高效而精致的单页面应用。

设计框架 ?

与传统的库 (e.g JQuery, Zepto) 不同,Angular 提供了一套声明式的编程规范帮助我们更规范,更高效地编写一个应用程序

开发平台?

和其他的声明式框架 (Vue, React) 不同,Angular 自身提供了丰富的 API 和解决方案(模块,请求,表单,路由,依赖注入。。。)来帮助我们完成几乎所有的业务能力上的开发 总体来说,Angular 是一个高度集成,API 丰富,设计极其优秀的框架级 Web 解决方案

Angular 的官网

学习 Angular 之前的前置知识:

  1. HTMLCSSECMAScriptDOMBOM
  2. TypeScript

说说怎么安装 Angular

这里主要介绍 脚手架 构建 Angular 项目

脚手架安装

  1. 全局安装 Angular 的脚手架 @angular/cli
shell 复制代码
# npm 安装
npm install -g @angular/cli

# yarn 安装
yarn global add @angular/cli

# pnpm 安装
pnpm install -g @angular/cli
  1. 使用脚手架命令创建一个 Angular 项目
shell 复制代码
# ng new xxx (xxx -> 项目名称)
ng new my-first-ng-project
  1. 打开项目,使用 ng serve 启动项目,启动完成后在浏览器输入 http://localhost:4200打开项目

Angular 脚手架构建出来的项目结构

shell 复制代码
my-first-ng-project
  |- .angular            # angular 脚手架的构建产物 (ng build)
  |- .vscode             # vscode 编辑器配置文件
  |- node_modules        # 项目依赖
  |- src                 # 项目源码
    |- app                        # app 组件文件夹
      |- app.component.html                       # App 组件的模板
      |- app.component.scss                       # App 组件的样式
      |- app.component.ts                         # App 组件的逻辑
      |- app.config.ts                            # App 组件的整体配置
    |- assets                     # 项目静态资源
    |- favicon.ico                # 项目图标
    |- index.html                 # 入口模板文件
    |- main.ts                    # 项目入口脚本
    |- styles.scss                # 全局演示文件
  |- .editorconfig       # 编辑器配置文件
  |- .gitignore          # git 提交时忽略什么
  |- package.json        # 项目依赖表
  |- README.md           # 脚手架生成的说明文件
  |- tsconfig.app.json   # app 中的 TypeScript 配置文件
  |- tsconfig.json       # 项目整体的 TypeScript 配置文件
  |- tsconfig.spec.json  # *.sepc 的 TypeScript 配置文件

写一个 Counter 小例子尝鲜

写一个 Counter 组件

  • counter.component.html
html 复制代码
<main class="main">
  <div class="container">
    Hello

    <div class="counter">
      <h1>{{ count }}</h1>

      <button (click)="addCount()">Add Count</button>
    </div>
  </div>
</main>
  • counter.component.scss
css 复制代码
.counter button {
  border: none;
  outline: none;
  padding: 6px 10px;
  box-sizing: border-box;
  box-shadow: 0 0 2px #ddd;
  border-radius: 3px;
}
  • counter.component.ts
ts 复制代码
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [CommonModule],
  templateUrl: './counter.component.html',
  styleUrl: './counter.component.scss',
})
export class AppComponent {
  public count: number = 0;

  addCount() {
    this.count += 1;
  }
}
  • counter.config.ts
ts 复制代码
  import { ApplicationConfig } from '@angular/core';

  export const appConfig: ApplicationConfig = {
    providers: []
  };

再写一个 TodoList

  • todo-list.component.html
html 复制代码
<div class="todo-list">
  <div class="add-input">
    <h1>{{ addInputValue }}</h1>

    <!-- 1. 数据的单向流 -->
    <!-- <input
      type="text"
      placeholder="请输入"
      [value]="addInputValue"
      (input)="handleInputChange($event)"
    /> -->

    <!-- 数据的双向绑定 -->
    <input
      type="text"
      placeholder="请输入待办项"
      [(ngModel)]="addInputValue"
    />
    <button (click)="handleAddTodo()">确认添加</button>
  </div>

  <ul class="list">
    <li
      class="list-item"
      *ngFor="let item of list"
    >
      <input type="checkbox" [checked]="item.completed" (change)="handleToggleTodo(item.id)" />
      <span class="">{{ item.content }}</span>
      <button (click)="handleRemoveTodo(item.id)">删除待办</button>
    </li>
  </ul>
</div>
  • todo-list.component.ts
ts 复制代码
import {
  Component,
  EventEmitter,
  Input,
  Output,
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

interface ITodo {
  id: number;
  content: string;
  completed: boolean;
}

@Component({
  selector: 'todo-list',
  standalone: true,
  imports: [CommonModule, FormsModule],
  templateUrl: './todo-list.component.html',
  styleUrl: 'todo-list.component.scss',
})
export default class TodoListComponent {
  @Input()
  public addInputValue: string = '';

  @Output()
  public addInputValueChange = new EventEmitter<string>();

  public list: ITodo[] = [];

  public handleAddTodo(): void {
    const content: string = this.addInputValue.trim();

    if (!content) {
      window.alert('添加的内容不能为空!');
      return;
    }

    const newItem: ITodo = {
      id: new Date().getTime(),
      content,
      completed: false,
    };
    this.list = [newItem, ...this.list];

    this.addInputValue = '';
  }

  public handleInputChange(e: Event): void {
    const el: HTMLInputElement = e.target as HTMLInputElement;
    const addInputValue: string = el.value;
    this.addInputValue = addInputValue;
  }

  public handleToggleTodo(id: number) {
    this.list = this.list.map(item => {
      if (item.id === id) {
        item.completed === !item.completed;
      }

      return item;
    });
  }

  public handleRemoveTodo(id: number) {
    this.list = this.list.filter(item => item.id !== id);
  }
}

最后

到这里就结束了,感觉还是蛮好用的,具体的 API 还是得都看看文档

相关推荐
程序员凡尘27 分钟前
完美解决 Array 方法 (map/filter/reduce) 不按预期工作 的正确解决方法,亲测有效!!!
前端·javascript·vue.js
编程零零七4 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
(⊙o⊙)~哦6 小时前
JavaScript substring() 方法
前端
无心使然云中漫步6 小时前
GIS OGC之WMTS地图服务,通过Capabilities XML描述文档,获取matrixIds,origin,计算resolutions
前端·javascript
Bug缔造者6 小时前
Element-ui el-table 全局表格排序
前端·javascript·vue.js
xnian_7 小时前
解决ruoyi-vue-pro-master框架引入报错,启动报错问题
前端·javascript·vue.js
麒麟而非淇淋8 小时前
AJAX 入门 day1
前端·javascript·ajax
2401_858120538 小时前
深入理解MATLAB中的事件处理机制
前端·javascript·matlab
阿树梢8 小时前
【Vue】VueRouter路由
前端·javascript·vue.js
随笔写9 小时前
vue使用关于speak-tss插件的详细介绍
前端·javascript·vue.js