각도 회로도란 무엇입니까? 구축하는 방법? (상해)

青灯夜游
풀어 주다: 2022-02-22 11:20:50
앞으로
2752명이 탐색했습니다.

Angular 회로도란 무엇인가요? Angular Schematics를 로컬로 개발하는 방법은 무엇입니까? 다음 기사에서는 자세한 소개를 제공하고 예제를 사용하여 더 잘 익숙해질 수 있기를 바랍니다.

각도 회로도란 무엇입니까? 구축하는 방법? (상해)

Angular Schematics란 무엇인가요?

Angular Schematics는 템플릿 기반의 Angular 전용 코드 생성기입니다. 물론 코드를 생성할 뿐만 아니라 Angular CLI를 기반으로 자체 자동화를 구현할 수도 있습니다. [관련 튜토리얼 추천: "angular tutorial"]

저는 Angular 프로젝트를 개발할 때 모든 사람들이 ng 생성 컴포넌트 컴포넌트 이름, ng @angular/materials 추가를 사용했다고 믿습니다. code>, ng generate module module-name, 이것들은 Angular에서 우리를 위해 구현된 일부 CLI인데, 우리 프로젝트에서 우리 자신의 프로젝트를 기반으로 CLI를 어떻게 구현해야 할까요? 이 글은 ng-devui-adminng generate component component-name, ng add @angular/materials, ng generate module module-name,这些都是 Angular 中已经为我们实现的一些 CLI,那么我们应该如何在自己的项目中去实现基于自己项目的 CLI 呢?本文将会基于我们在 ng-devui-admin 中的实践来进行介绍。欢迎大家持续的关注,后续我们将会推出更加丰富的 CLI 帮助大家更快搭建一个 Admin 页面。

如何在本地开发你的 Angular Schematics

在本地开发你需要先安装 schematics 脚手架

npm install -g @angular-devkit/schematics-cli

# 安装完成之后新建一个schematics项目
schematics blank --name=your-schematics
로그인 후 복사

新建项目之后你会看到如下目录结构,代表你已经成功创建一个 shematics 项目。

각도 회로도란 무엇입니까? 구축하는 방법? (상해)

重要文件介绍

  • tsconfig.json: 主要与项目打包编译相关,在这不做具体介绍

  • collection.json:与你的 CLI 命令相关,用于定义你的相关命令

{
  "$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
  "schematics": {
    "first-schematics": {
      "description": "A blank schematic.",
      "factory": "./first-schematics/index#firstSchematics"
    }
  }
}
로그인 후 복사

first-schematics: 命令的名字,可以在项目中通过 ng g first-schematics:first-schematics 来运行该命令。description: 对该条命令的描述。factory에서의 관행을 바탕으로 소개됩니다. 앞으로도 여러분의 지속적인 관심을 환영합니다. 관리 페이지를 더욱 빠르게 구축할 수 있도록 더욱 풍부한 CLI를 출시하겠습니다. schema,我们将在后面进行讲解。

  • index.ts:在该文件中实现你命令的相关逻辑
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';

export function firstSchematics(_options: any): Rule {
  return (tree: Tree, _context: SchematicContext) => {
    return tree;
  };
}
로그인 후 복사

在这里我们先看几个需要了解的参数:tree:在这里你可以将 tree 理解为我们整个的 angular 项目,你可以通过 tree 新增文件,修改文件,以及删除文件。_context:该参数为 schematics 运行的上下文,比如你可以通过 context 执行 npm installRule:为我们制定的操作逻辑。

实现一个 ng-add 指令

现在我们通过实现一个 ng-add 指令来更好的熟悉。

同样是基于以上我们已经创建好的项目。

新建命令相关的文件

首先我们在 src 目录下新建一个目录 ng-add,然后在该目录下添加三个文件 index.ts, schema.json, schema.ts,之后你的目录结构应该如下:

각도 회로도란 무엇입니까? 구축하는 방법? (상해)

配置 <span style="font-size: 18px;">collection.json</span>

之后我们在 collection.json 中配置该条命令:

{
  "$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
  "schematics": {
    ...,
    "ng-add": {
      "factory": "./ng-add/index",
      "description": "Some description about your schematics",
      "schema": "./ng-add/schema.json"
    }
  }
}
로그인 후 복사

<span style="font-size: 18px;">files</span> 目录中加入我们想要插入的文件

关于 template 的语法可以参考 ejs 语法

app.component.html.template

<div class="my-app">
  <% if (defaultLanguage === &#39;zh-cn&#39;) { %>你好,Angular Schematics!<% } else { %>Hello, My First Angular Schematics!<% } %>
  <h1>{{ title }}</h1>
</div>
로그인 후 복사

app.component.scss.template

.app {
  display: flex;
  justify-content: center;
  align-item: center;
}
로그인 후 복사

app.component.ts.template

import { Component } from &#39;@angular/core&#39;;

@Component({
  selector: &#39;app-root&#39;,
  templateUrl: &#39;./app.component.html&#39;,
  styleUrls: [&#39;./app.component.scss&#39;]
})
export class AppComponent {
  title = <% if (defaultLanguage === &#39;zh-cn&#39;) { %>&#39;你好&#39;<% } else { %>&#39;Hello&#39;<% } %>;
}
로그인 후 복사

开始实现命令逻辑

  • schema.json:在该文件中定义与用户的交互
{
  "$schema": "http://json-schema.org/schema",
  "id": "SchematicsDevUI",
  "title": "DevUI Options Schema",
  "type": "object",
  "properties": {
    "defaultLanguage": {
      "type": "string",
      "description": "Choose the default language",
      "default": "zh-cn",
      "x-prompt": {
        "message": "Please choose the default language you want to use: ",
        "type": "list",
        "items": [
          {
            "value": "zh-cn",
            "label": "简体中文 (zh-ch)"
          },
          {
            "value": "en-us",
            "label": "English (en-us)"
          }
        ]
      }
    },
    "i18n": {
      "type": "boolean",
      "default": true,
      "description": "Config i18n for the project",
      "x-prompt": "Would you like to add i18n? (default: Y)"
    }
  },
  "required": []
}
로그인 후 복사

在以上的定义中,我们的命令将会接收两个参数分别为 defaultLanguagei18n,我们以 defaultLanguage

로컬에서 Angular Schematics를 개발하는 방법🎜🎜로컬에서 개발하려면 먼저 schematics 스캐폴딩을 설치해야 합니다🎜
{
  "defaultLanguage": {
    "type": "string",
    "description": "Choose the default language",
    "default": "zh-cn",
    "x-prompt": {
      "message": "Please choose the default language you want to use: ",
      "type": "list",
      "items": [
        {
          "value": "zh-cn",
          "label": "简体中文 (zh-ch)"
        },
        {
          "value": "en-us",
          "label": "English (en-us)"
        }
      ]
    }
  }
}
로그인 후 복사
로그인 후 복사
🎜새 프로젝트를 생성한 후 다음 디렉터리 구조를 볼 수 있습니다. shematics 프로젝트를 성공적으로 생성했습니다. 🎜🎜각도 회로도란 무엇입니까? 구축하는 방법? (상해)🎜🎜중요 파일 소개🎜
관련 라벨:
원천:juejin.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!