试用 Angular v6 的 Ivy compiler
“Ivy” 是 Angular v6 的新一代渲染器。从 v6.0.0-beta.1 开始,Ivy 已经作为体验 API 发布。
作为下一代的 Angular 的视图引擎,重点在于彻底缩减代码尺寸并增强灵活性。在这个示例中,你可以看到,对于一个 Hello, world 应用,代码的尺寸可以缩减到 3K 左右。
创建项目
使用 ng new --minimal 创建一个最小化项目。
ng new ngv6-ivy --minimal
更新 Angular 到 v6
将所有的 Angular 包更新到 v6. 当前的 package.json 内容。 当前版本是 v6.0.0 beta.3.
{
"name": "ngv6-ivy",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build --prod",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "6.0.0-beta.3",
"@angular/common": "6.0.0-beta.3",
"@angular/compiler": "6.0.0-beta.3",
"@angular/core": "6.0.0-beta.3",
"@angular/forms": "6.0.0-beta.3",
"@angular/http": "6.0.0-beta.3",
"@angular/platform-browser": "6.0.0-beta.3",
"@angular/platform-browser-dynamic": "6.0.0-beta.3",
"@angular/router": "6.0.0-beta.3",
"core-js": "^2.4.1",
"rxjs": "^5.5.6",
"zone.js": "^0.8.19"
},
"devDependencies": {
"@angular/cli": "1.6.8",
"@angular/compiler-cli": "6.0.0-beta.3",
"@angular/language-service": "6.0.0-beta.3",
"typescript": "~2.5.3"
}
}
使用 ng version 检查当前项目
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/ Angular CLI: 1.6.
Node: 8.9.
OS: win32 x64
Angular: 6.0.-beta.
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router @angular/cli: 1.6.
@angular-devkit/build-optimizer: 0.0.
@angular-devkit/core: 0.0.
@angular-devkit/schematics: 0.0.
@ngtools/json-schema: 1.1.
@ngtools/webpack: 1.9.
@schematics/angular: 0.1.
typescript: 2.5.
webpack: 3.10.
启用 Ivy
1. 在 src/tsconfig.app.json 中添加 enableIvy , See Angular ChangeLog
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"baseUrl": "./",
"module": "es2015",
"types": []
},
"exclude": [
"test.ts",
"**/*.spec.ts"
],
"angularCompilerOptions": {
"enableIvy": true
}
}
2. 从 AppModule 中删除 BrowserModule
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
3. 简化 AppComponent
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: 'Hello {{greeting}}!',
})
export class AppComponent {
greeting = 'World';
}
4. 添加 ngc 命令到 package.json
{
"name": "ngv6-ivy",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build --prod",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"ngc": "ngc -p src/tsconfig.app.json"
}
5. 在 src/tsconfig.json 中将 target 设置为 es2016:
"target": "es2016",
运行 ngc
npm run ngc -p src/tsconfig.app.json
查看输出结果
输出结果在 tsc-out 目录中。
检查 Ivy: ngComponentDef
打开 tsc-out/app/src/app/app.component.js
import { Component } from '@angular/core';
import * as i0 from '@angular/core';
export class AppComponent {
constructor() {
this.greeting = 'World';
}
}
AppComponent.decorators = [
{
type: Component,
args: [
{
selector: 'app-root',
template: 'Hello {{greeting}}!'
}
]
}
];
/** @nocollapse */
AppComponent.ctorParameters = () => [];
AppComponent.ngComponentDef = i0.ɵdefineComponent({
tag: 'app-root',
factory: function AppComponent_Factory() {
return new AppComponent();
},
template: function AppComponent_Template(ctx, cm) {
if (cm) {
i0.ɵT(0);
}
i0.ɵt(0, i0.ɵb1('Hello ', ctx.greeting, '!'));
}
});
//# sourceMappingURL=app.component.js.map
注意 AppComponent.ngComponentDef,它使用 Ivy API 定义。
template 函数是从组件的 HTML 模板生成。
当 Ivy 稳定之后,我们将可以在组件中编写这些定义。然后,当前的 HTML 模板将会是可选的。我们可以选择无装饰的组件,它使用纯的 JavaScript 编写。
你可以在 GitHub 下载到完整的项目内容:https://github.com/lacolaco/ngv6-ivy
相关资料
试用 Angular v6 的 Ivy compiler的更多相关文章
- Angular v6 正式发布
Angular 6 正式发布 Angular 6 已经正式发布了!这个主要版本并不关注于底层的框架,更多地关注于工具链,以及使 Angular 在未来更容易快速推进. 作为发布的一部分,我们同步了主要 ...
- 在 Angular 8 中,我们可以期待些什么
转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 本文由葡萄城翻译并发布 --- Angular 作为一款优秀的前端框架,自诞生之日起,就致力于面向前端开发者 ...
- [转]Angular: Hide Navbar Menu from Login page
本文转自:https://loiane.com/2017/08/angular-hide-navbar-login-page/ In this article we will learn two ap ...
- [转]How to Add Bootstrap to an Angular CLI project
本文转自:https://loiane.com/2017/08/how-to-add-bootstrap-to-an-angular-cli-project/ In this article we w ...
- [转]Using Angular in Visual Studio Code
本文转自:https://code.visualstudio.com/docs/nodejs/angular-tutorial Using Angular in Visual Studio Code ...
- [Angular] Angular Elements Intro
Make sure install the latest Angular v6 with Angular CLI. Checkout ght Github for the code. 1. Creat ...
- 用angular做的模糊搜索
今天大家来试一试用angular做一下简单的搜索功能吧: 首先我们需要写html的部分,我们需要设置几个条件,比如按什么来排序,按升序还是降序搜索,和一个文本框来设置模糊搜索: <nav> ...
- Angular6.0发布
Angular v6 新版本重点关注工具链以及工具链在 Angular 中的运行速度问题. Angular v6 是统一整体框架.Material 和 CLI 三大 Angular 组件的第一个版本, ...
- AngularJS+requireJS项目的目录结构设想
AngularJS+requireJS项目的目录结构设想 准备用AngularJS + require.js 作为新项目的底层框架,以下目录结果只是一个初步设想: /default 放页面,不过 ...
随机推荐
- expdp用户10迁移到新环境11之正式实施
expdp迁移源端数据库:cu 源端IP: 源端schema: xxx目标数据库:ora 目标IP:xxx操作流程:31日凌晨应用停,随后使用数据泵迁移,两套库迁移,迁移一套,迁移完毕应用确 ...
- Unity VS 创建脚本自动添加头注释-时间-描述-作者等信息
Unity生成脚本自动添加头注释 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心 ...
- python学习6---排序问题
1.对列表排序 一维列表: sorted():可用于任何可迭代对象,如数组.列表.字典等. sort():list.sort()返回None,这是因为sort在函数内部修改了list的值,当再次访问l ...
- 分别用命令行、NetBeans IDE 8.2实现firstcup 项目部署
准备工作要搞好,对吧!(如下:) firstcup项目代码文件下载链接:click me~ NetBeans IDE 8.2下载链接:点我~(ps:建议下载此版本,再安装过程中,要选择安装GlassF ...
- flask中需要的基本配置信息
1.秘钥设置: app.secret_key = '随意设置' 2.SQLALCHEMY配置: # 连接数据库 app.config['SQLALCHEMY_DATABASE_URI'] = 'mys ...
- E - 改革春风吹满地
按顺时针或者逆时针顺序输入n个点,求输入点围城的多边形的面积.凸凹都可以计算. 模板 #include <iostream> #include <cstring> #inclu ...
- es6里面的arr方法
es6里面,关于arr的遍历以及查找,新增了很多的方法,对于不同的应用场景,运用合适的方法,可以达到事半功倍的效果: 一, arr.find():用于查找到符合条件的第一个成员,如果没有查找到的话,则 ...
- Alisha’s Party (HDU5437)优先队列+模拟
Alisha 举办聚会,会在一定朋友到达时打开门,并允许相应数量的朋友进入,带的礼物价值大的先进,最后一个人到达之后放外面的所有人进来.用优先队列模拟即可.需要定义朋友结构体,存储每个人的到达顺序以及 ...
- Centos6.5下使用LAMP搭建discuz论坛(编译安装 PS :自学中 写的不好请见谅)
wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.2.27.tar.gz 下载Apache软件包 wget http://downloads.my ...
- Ubuntu使用命令行打印文件
Ubuntu使用命令行打印文件 正文 环境: Ubuntu 16.04.3 LTS HP Deskjet InkAdvantage 4648 准备步骤 安装Common UNIX Printing S ...