试用 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 放页面,不过 ...
随机推荐
- centos7下部署node应用程序
一.安装node 二.安装nginx 三.使用express写一个简单的demo,并且使用pm2部署 四.错误 invalid PID number "" in "/ru ...
- CSS选择器中的特殊性
我们来看一下一个简单的例子: <!DOCTYPE html><html lang="en"><head> <meta charset=&q ...
- 支持pc和移动端的手写签批功能
由于之前的业务需要,要求在pc端(用鼠标写字).移动端(手写)实现会签功能,然后百度下载了个签字插件,经过一些修改和功能添加,实现了现有的功能插件,效果如图: 代码下载地址:https://githu ...
- springboot+mybatis-puls利用swagger构建api文档
项目开发常采用前后端分离的方式.前后端通过API进行交互,在Swagger UI中,前后端人员能够直观预览并且测试API,方便前后端人员同步开发. 在SpringBoot中集成swagger,步骤如下 ...
- vlookup使用
数据处理过程中,需要excel进行简单的操作,比如vlookup,摸索之后,总结如下:
- 离线安装Eclipse插件-Vrapper
首先下载Vrapper的资源文件:https://sourceforge.net/projects/vrapper/ 下载完成后解压,将features和plugins文件夹内的文件复制到eclips ...
- ASP.NET+MVC+EntityFramework快速实现增删改查
本教程已经录制视频,欢迎大家观看我在CSDN学院录制的课程:http://edu.csdn.net/lecturer/944
- the status bar issue of react-native Modal on Android ( RN v0.57.0)
Problem: When use Modal in react-native, the status bar is not included if you make a full-screen ma ...
- PymongoDB_study
import pymongo client = pymongo.MongoClient(host='localhost',port=27017)#连接数据库 #db = client.test#指定数 ...
- R实用小技巧
输出重定向 # 文本重定向 # cat cat("hello",file="D:/test.txt", append=T) # sink("filen ...