“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 Ivy code size

Ivy 演示页面

试用 Angular v6 的 Ivy compiler的更多相关文章

  1. Angular v6 正式发布

    Angular 6 正式发布 Angular 6 已经正式发布了!这个主要版本并不关注于底层的框架,更多地关注于工具链,以及使 Angular 在未来更容易快速推进. 作为发布的一部分,我们同步了主要 ...

  2. 在 Angular 8 中,我们可以期待些什么

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 本文由葡萄城翻译并发布 --- Angular 作为一款优秀的前端框架,自诞生之日起,就致力于面向前端开发者 ...

  3. [转]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 ...

  4. [转]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 ...

  5. [转]Using Angular in Visual Studio Code

    本文转自:https://code.visualstudio.com/docs/nodejs/angular-tutorial Using Angular in Visual Studio Code ...

  6. [Angular] Angular Elements Intro

    Make sure install the latest Angular v6 with Angular CLI. Checkout ght Github for the code. 1. Creat ...

  7. 用angular做的模糊搜索

    今天大家来试一试用angular做一下简单的搜索功能吧: 首先我们需要写html的部分,我们需要设置几个条件,比如按什么来排序,按升序还是降序搜索,和一个文本框来设置模糊搜索: <nav> ...

  8. Angular6.0发布

    Angular v6 新版本重点关注工具链以及工具链在 Angular 中的运行速度问题. Angular v6 是统一整体框架.Material 和 CLI 三大 Angular 组件的第一个版本, ...

  9. AngularJS+requireJS项目的目录结构设想

    AngularJS+requireJS项目的目录结构设想 准备用AngularJS + require.js 作为新项目的底层框架,以下目录结果只是一个初步设想: /default    放页面,不过 ...

随机推荐

  1. linux终端窗口字体缩放快捷键

    环境:ubuntu16.04, 打开终端,有时候log输出一行显示不下 ‘ctrl’ + ‘-’字体缩小,一行显示更多的内容 ‘ctrl’ + ‘shift’ + ‘+’字体变大

  2. 开发Canvas 绘画应用(四):实现拖拽绘画

    在开发Canvas绘画应用(三):实现对照绘画中,我们实现了视图引导的第一部分,这一篇我们来完成第二部分,即将图片直接拖到画布上进行绘画. ✁ 拖放如何实现? [拖放的基本概念]:创建一个绝对定位的元 ...

  3. 测试那些事儿-Jmeter介绍及使用

    Jmeter与LR有啥区别? Jmeter工具组成部分: 1.资源生成器:用于生成测试过程中服务器,负载机的资源代码.(LR中的VuGen) 2.用户运行器:通常是一个脚本运行引擎,根据脚本要求模拟指 ...

  4. Java基于opencv—矫正图像

    更多的时候,我们得到的图像不可能是正的,多少都会有一定的倾斜,就比如下面的 我们要做的就是把它们变成下面这样的 我们采用的是寻找轮廓的思路,来矫正图片:只要有明显的轮廓都可以采用这种思路 具体思路: ...

  5. 20164322 韩玉婷-----Exp6 信息搜索与漏洞扫描

    1.实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 2.实践内容 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具 ...

  6. PXE高效能批量网络装机

    PXE简绍 PXE(preboot execute environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过网络从远端服务器 ...

  7. JSF生命周期&Facelets的生命周期

    1.JSF生命周期 1)恢复视图(Restore View) 视图表示组成特定页面的所有组件.它被保存在 客户端(通常存储在隐藏字段中)或服务器中(通常在会话中).根据请求访问的视图ID(页面地址), ...

  8. C# 代码补全

    cw + Tab + Tab           输出 Console.WriteLine(); try +Tab+Tab           输出 try catch代码块 foreach + Ta ...

  9. 简述linux操作系统启动流程

    Linux启动流程 POST-->BootSequence(BIOS)->Bootloader(MBR,grub)-->kernnel(ramdisk,initrd)-->ro ...

  10. 普通Linux用户1分钟上手vi编辑器

    *导读:普通用户只要花1分钟看第二部分即可.高级用户请忽略本文* 目录 1. 编辑器之战 2. vi的使用 2.1 vi的3个模式 2.2 vi的3个模式切换 2.3 vi最基本的命令 2.4 vi的 ...