[Schematics] 1. Copy and Manipulate Template
1. Create a src/my-component/files/src/app directory to hold your templates.
mkdir -p src/my-component/files/src/app
2. Create an app.component.ts file in src/my-component/files/src/app and put the following code in it:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
name = '<%= name %>';
}
The <%= name %> variable is an option you’ll pass in when running this Schematic. Create an app.component.html file with some HTML that reads the name variable.
3. Add template as well:
<div style="text-align:center">
<h1>
Hello, {{ name }}
</h1>
</div> <router-outlet></router-outlet>
4. Now what we want is let user to give the 'name' thought prompt, In order to define the name prompt, create a schema.json file in the src/my-component directory.
{
"$schema": "http://json-schema.org/schema",
"id": "SchematicsMyComponent",
"title": "My Component Schema",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Your Name",
"x-prompt": "What is your name?"
}
},
"required": ["name"]
}
Then update src/collection.json to reference this file in a schema property.
{
"$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
"schematics": {
"my-component": {
"description": "A blank schematic.",
"factory": "./my-component/index#myComponent",
"schema": "./my-component/schema.json"
}
}
}
5. Then update src/my-component/index.ts so you can get your generated project’s path, and copy templates.
import {
apply,
MergeStrategy,
mergeWith,
move,
Rule,
SchematicContext,
template,
Tree,
url,
FileEnty,
forEach
} from '@angular-devkit/schematics';
import { join, normalize } from 'path';
import { getWorkspace } from '@schematics/angular/utility/config';
export function setupOptions(host: Tree, options: any): Tree {
const workspace = getWorkspace(host);
if (!options.project) {
options.project = Object.keys(workspace.projects)[];
}
const project = workspace.projects[options.project];
options.path = join(normalize(project.root), 'src');
return host;
}
export function myComponent(_options: any): Rule {
return (tree: Tree, _context: SchematicContext) => {
setupOptions(tree, _options);
const movePath = normalize(_options.path + '/');
const templateSource = apply(url('./files/src'), [
template({..._options}),
move(movePath),
// fix for https://github.com/angular/angular-cli/issues/11337
forEach((fileEntry: FileEntry) => {
if (tree.exists(fileEntry.path)) {
tree.overwrite(fileEntry.path, fileEntry.content);
}
return fileEntry;
}),
]);
const rule = mergeWith(templateSource, MergeStrategy.Overwrite);
return rule(tree, _context);
};
}
We can use this as a template when we want to add some template into the schematics.
6. Test with real application:
ng new my-test-app --routing --style css
cd my-test-app
npm link ../my-component
ng g my-component:my-component
When I tried this, it prompted me:
$ ng g my-component:my-component
? What is your name? Matt
UPDATE src/app/app.component.html ( bytes)
UPDATE src/app/app.component.ts ( bytes)
7. Support for ng add with Angular CLI:
A slick feature of Angular CLI is its ng add command. You can use it to invoke schematics and add features like PWA support and Angular Material to your projects. For example:
ng add @angular/pwa
ng add @angular/material
You can support for ng add $your-schematic too! Open my-component/src/collection.json and add a new ng-add schematic.
{
"$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
"schematics": {
"my-component": {
"description": "A blank schematic.",
"factory": "./my-component/index#myComponent",
"schema": "./my-component/schema.json"
},
"ng-add": {
"factory": "./ng-add/index",
"description": "Add schematic",
"schema": "./my-component/schema.json"
}
}
}
Create src/ng-add/index.ts and add the code necessary for it to invoke the my-component schematic.
import { chain, Rule, schematic, SchematicContext, Tree, } from '@angular-devkit/schematics';
export default function (options: any): Rule {
return (host: Tree, context: SchematicContext) => {
return chain([
schematic('my-component', options)
])(host, context);
};
}
After build, link to project, then you can run:
ng add my-component
[Schematics] 1. Copy and Manipulate Template的更多相关文章
- 在库中使用schematics——ng add与ng update
起步 创建一个angular库 ng new demo --create-application=false ng g library my-lib 可见如下目录结构 ├── node_modules ...
- vscode c++ cmake template project
VSCode configure C++ dev environment claim use CMake to build the project. For debugging, VSCode's C ...
- DataGridView控件
DataGridView控件 DataGridView是用于Windows Froms 2.0的新网格控件.它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我们的用户需要的特 ...
- DataGridView控件-[引用]
DataGridView控件 DataGridView是用于Windows Froms 2.0的新网格控件.它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我们的用户需要的特 ...
- DataGridView控件使用大全说明-各种常用操作与高级操作
DataGridView控件 DataGridView是用于Windows Froms 2.0的新网格控件.它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我们的用户需要的特 ...
- DataGridView控件使用大全
转自:http://www.cnblogs.com/xiaofengfeng/archive/2011/04/16/2018504.html DataGridView控件 DataGridView是用 ...
- HEC-ResSim原文档
HEC-ResSim Reservoir System Simulation User's Manual Version 3.1 May 201 ...
- Apache Spark 1.6 Hadoop 2.6 Mac下单机安装配置
一. 下载资料 1. JDK 1.6 + 2. Scala 2.10.4 3. Hadoop 2.6.4 4. Spark 1.6 二.预先安装 1. 安装JDK 2. 安装Scala 2.10.4 ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q121-Q123)
Question 121 You are designing a SharePoint 2010 workflow that will be used to monitor invoices. Th ...
随机推荐
- json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (ch
阐述 想要把一个字符串转化成字典对象,在使用json的过程出现如此报错 解决方法 将字符串里面的单引号改为双引号
- 启动Spring boot项目报错:java.lang.IllegalArgumentException: LoggerFactory is not a Logback
java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on t ...
- C#破解dll
使用反编译工具对dll文件进行反编译,找到校验过期的相关代码,反编译工具可以使用ILSpy或Reflector; 使用ildasm.exe工具将dll导出成il文本文件,在该文件中找到相关的代码进行修 ...
- DHCP服务器的架设
DHCP服务器的架设 一.DHCP服务器的安装要求: 搭建DHCP服务器需要一些必备条件支持,主要有以下方面: 需要一台运行Windows Server系统的服务器,并为其指定静态IP地址. 根据子网 ...
- Qt 子线程更新Ui
最近做练习,写一个Qt版的飞机大战,需要用子线程更新UI,发现Qt子线程不能更新Ui,否则程序会崩溃.在网上百度了下,说是需要在子线程自定义信号,然后在线程回调的run()函数里发射信号,主线程连接信 ...
- 安卓MediaPlayer框架之Binder机制
Binder简介 Binder是Android系统进程间通信的主要方式之一. 1.在ASOP中,Binder使用传统的C/S通信方式:即一个进程作为服务端提供诸如视音频解封装,解码渲染,地址查询等各种 ...
- [LeetCode] 63. 不同路径 II ☆☆☆(动态规划)
描述 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 现在 ...
- redis键过期 (redis 2.6及以上)
EXPIRE key seconds 用来对一个键设置一个过期时间,第二个参数表示经过多少秒后键过期. 一个键过期后, 这个键将会被自动删除. 在Redis术语中,带有过期时间的键经常被称作volat ...
- Keil、uVision、RealView、MDK、Keil C51之间的区别比较
我们要区别的概念:KEIL UVision,KEIL MDK,KEIL For ARM,RealView MDK,KEIL C51,KEIL C166,KEIL C251 从接触MCS-51单片机开始 ...
- ssh无密码连接
1. 生成密钥对文件 [root@centos2 ~]# -t 指定加密类型 -b 指定密钥对加密长度 询问1:执行过程中会询问保存位置,一般默认保存在当前用户家目录下的.ssh/目录下 询问2:是否 ...