Angular 个人深究【Angular中的Typescript 装饰器】

最近进入一个新的前端项目,为了能够更好地了解Angular框架,想到要研究底层代码。


注:本人前端小白一枚,文章旨在记录自己的学习过程,如有大神发现错误,也请评论指正。

Angular 中的装饰器

当安装好Angular后,打开 文件[/my-app/src/app/app.module.ts] 与文件 [/my-app/src/app/app.component.ts]

//app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
}) export class AppComponent { title = 'app';
}
//app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { AppComponent } from './app.component';
import { Test1Component } from './test1/test1.component';
import { Test2Component } from './test2/test2.component'; @NgModule({
declarations: [
AppComponent,
Test1Component,
Test2Component
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

两个文件中的@Component与 @Ngmodule 就是我们今天要说的typescript的装饰器的用法。

Typescript装饰器的由来

1) 经过查询资料,装饰器的概念来自于Python(本人Python也是小白)

def decorator(target):
print("You are decorated!")
return targett
#decorator是可以自定义的
@decorator
def yourfunction:
print("This is your function!")
yourfunction() #一下是运行结果
#You are decorated!
#This is your function! #网上还有分析,以上的代码的等价写法有助于理解。

2) ES6 引入了装饰器这个功能。

3) Angular 中 装饰器的测试

注:查询网上说Nodejs 与 Typescript都能支持装饰器,但是本人没有调试成功,以后扩展。

  •   装饰【类】的装饰器:

//test1.component.ts
import { Component, OnInit } from '@angular/core';
function test (target) {
console.log(target);
target.title = "app1";
return target;
}
//装饰一个类
@test
@Component({
selector: 'app-test1',
templateUrl: './test1.component.html',
styleUrls: ['./test1.component.css']
})
export class Test1Component implements OnInit {
title = 'app';
constructor() {}
ngOnInit() {}
}
console.log(Test1Component.title); //输出结果是app1
  •   装饰【类的方法、属性】的装饰器

import { Component, OnInit } from '@angular/core';

function readonly(flag:boolean){
return function readonly(target, name, descriptor) {
console.log(descriptor);
descriptor.writable = flag;
return descriptor;
}
}
export class Test1Component implements OnInit {
ngOnInit() {
this.test = function(){console.log("change")};
}
@readonly(false)
test(){
console.log("inner test function");
}
}
//控制台报错:ERROR TypeError: Cannot assign to read only property 'test' of object '[object Object]'
//打印的descriptor {value: ƒ, writable: true, enumerable: true, configurable: true}
/* 属性与方法都是一样的 */

Angular中的装饰器解读

  • 【NgModule 装饰器】

代码位置:node_modules\@angular\core\fesm5\core.js line1436(直接搜索var NgModule =)

/**
* NgModule decorator and metadata.
*
*
* @Annotation
*/
var NgModule = makeDecorator('NgModule', function (ngModule) { return ngModule; }, undefined, undefined, function (moduleType, metadata) {
var imports = (metadata && metadata.imports) || [];
if (metadata && metadata.exports) {
imports = __spread(imports, [metadata.exports]);
}
moduleType.ngInjectorDef = defineInjector({
factory: convertInjectableProviderToFactory(moduleType, { useClass: moduleType }),
providers: metadata && metadata.providers,
imports: imports,
});
});

格式:

@NgModule({
providers: Provider[]//Defines the set of injectable objects that are available in the injector of this module.
declarations: Array<Type<any> | any[]>//Specifies a list of directives/pipes that belong to this module
imports: Array<Type<any> | ModuleWithProviders | any[]>//Specifies a list of modules whose exported directives/pipes should be available to templates in this module. This can also contain ModuleWithProviders
exports: Array<Type<any> | any[]>//Specifies a list of directives/pipes/modules that can be used within the template of any component that is part of an Angular module that imports this Angular module.
entryComponents: Array<Type<any> | any[]>//Specifies a list of components that should be compiled when this module is defined. For each component listed here, Angular will create a ComponentFactory and store it in the ComponentFactoryResolver.
bootstrap: Array<Type<any> | any[]>//Defines the components that should be bootstrapped when this module is bootstrapped. The components listed here will automatically be added to entryComponents.
schemas: Array<SchemaMetadata | any[]>//Elements and properties that are not Angular components nor directives have to be declared in the schema.
id: string//An opaque ID for this module, e.g. a name or a path. Used to identify modules in getModuleFactory. If left undefined, the NgModule will not be registered with getModuleFactory.
})
//更多内容请访问官网
//https://www.angular.cn/api/core/NgModule
//https://www.angular.cn/guide/architecture-modules
//查看

代码解读:

function makeDecorator(name, props, parentClass, chainFn, typeFn) {var metaCtor = makeMetadataCtor(props);
function DecoratorFactory() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}if (this instanceof DecoratorFactory) {
       // 将使用装饰器的时候传入的参数放到descratorFactory
       //推荐https://www.jianshu.com/p/00dc4ad9b83f 去查看 .call.apply的作用
       //__spread 方法定义在tslib.js中, 将数组元素经过__read方法后concat
metaCtor.call.apply(metaCtor, __spread([this], args));return this;
}
var annotationInstance = new ((_a = DecoratorFactory).bind.apply(_a, __spread([void 0], args)))();
var TypeDecorator = function TypeDecorator(cls) {
typeFn && typeFn.apply(void 0, __spread([cls], args));
// Use of Object.defineProperty is important since it creates non-enumerable property which
// prevents the property is copied during subclassing.
var annotations = cls.hasOwnProperty(ANNOTATIONS) ?
cls[ANNOTATIONS] :
Object.defineProperty(cls, ANNOTATIONS, { value: [] })[ANNOTATIONS];
annotations.push(annotationInstance);
return cls;
};
if (chainFn)
chainFn(TypeDecorator);
return TypeDecorator;
var _a;
}
if (parentClass) {
DecoratorFactory.prototype = Object.create(parentClass.prototype);
}
DecoratorFactory.prototype.ngMetadataName = name;
DecoratorFactory.annotationCls = DecoratorFactory;
  //最后返回 DecoratorFactory
return DecoratorFactory;
}

注:期待有大神能够看到我的文章,并提出我的问题所在,小白一枚如果有错误还望指正。在此谢过。

 参考网址:

http://es6.ruanyifeng.com/#docs/decorator
http://web.jobbole.com/88572/
https://www.cnblogs.com/Wayou/p/es6_new_features.html#2917341

https://www.jianshu.com/p/00dc4ad9b83f

 

Angular 个人深究(一)【Angular中的Typescript 装饰器】的更多相关文章

  1. 从C#到TypeScript - 装饰器

    总目录 从C#到TypeScript - 类型 从C#到TypeScript - 高级类型 从C#到TypeScript - 变量 从C#到TypeScript - 接口 从C#到TypeScript ...

  2. 基于TypeScript装饰器定义Express RESTful 服务

    前言 本文主要讲解如何使用TypeScript装饰器定义Express路由.文中出现的代码经过简化不能直接运行,完整代码的请戳:https://github.com/WinfredWang/expre ...

  3. TypeScript 装饰器的执行原理

    装饰器本质上提供了对被装饰对象 Property​ Descriptor 的操作,在运行时被调用. 因为对于同一对象来说,可同时运用多个装饰器,然后装饰器中又可对被装饰对象进行任意的修改甚至是替换掉实 ...

  4. Python中利用函数装饰器实现备忘功能

    Python中利用函数装饰器实现备忘功能 这篇文章主要介绍了Python中利用函数装饰器实现备忘功能,同时还降到了利用装饰器来检查函数的递归.确保参数传递的正确,需要的朋友可以参考下   " ...

  5. python 中多个装饰器的执行顺序

    python 中多个装饰器的执行顺序: def wrapper1(f1): print('in wrapper1') def inner1(*args,**kwargs): print('in inn ...

  6. 第7.17节 Python类中的静态方法装饰器staticmethod 定义的静态方法深入剖析

    第7.17节  Python类中的静态方法装饰器staticmethod 定义的静态方法深入剖析 静态方法也是通过类定义的一种方法,一般将不需要访问类属性但是类需要具有的一些能力可以静态方法提供. 一 ...

  7. 第7.26节 Python中的@property装饰器定义属性访问方法getter、setter、deleter 详解

    第7.26节 Python中的@property装饰器定义属性访问方法getter.setter.deleter 详解 一.    引言 Python中的装饰器在前面接触过,老猿还没有深入展开介绍装饰 ...

  8. 使用 Vue + TypeScript 时项目中常用的装饰器

    目录 一.@Component 装饰器 1)父组件 2)子组件 二. @Emit 装饰器 1)父组件 2)子组件 三. @Model 装饰器 1)父组件 2)子组件 四. @Prop 装饰器 1)父组 ...

  9. TypeScript装饰器(decorators)

    装饰器是一种特殊类型的声明,它能够被附加到类声明,方法, 访问符,属性或参数上,可以修改类的行为. 装饰器使用 @expression这种形式,expression求值后必须为一个函数,它会在运行时被 ...

随机推荐

  1. C/C++如何整行读入字符串?

    在写代码的过程中,经常要一行一行的读入字符串进行处理,这里我总结了一些方法,供大家参考. 方法一:scanf()读入char[] 使用方法: ]; scanf("%[^\n]",& ...

  2. SPOJ6340 ZUMA - ZUMA

    题意:n个珠子排成一排,都有各自的颜色. 你可以选择不少于w个连续同色的珠子消掉,也可以先放着.你还可以任意插入任意颜色的珠子. 求全部消掉至少要插入几个珠子. 解: 什么毒瘤东西...... 有个十 ...

  3. 《剑指offer》— JavaScript(32)把数组排成最小的数

    把数组排成最小的数 题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为3213 ...

  4. HTML培训课程-------Day02(表格和框架)

    表格 在网页中表格是一种经常使用到得设计结构,就像表格的内容中可以包含任何的数据,如文字.图像.表单.超链接.表格等等,所有在HTML中可以使用的数据,都可以被设置在表格中,所以有关表格设置的标记与属 ...

  5. 异步消息处理机制Handler

    Android 中的异步消息处理主要由四个部分组成,Message.Handler.MessageQueue 和Looper. 1. Message Message 是在线程之间传递的消息,它可以在内 ...

  6. bzoj千题计划257:bzoj4199: [Noi2015]品酒大会

    http://www.lydsy.com/JudgeOnline/problem.php?id=4199 求出后缀数组的height 从大到小枚举,合并 维护组内 元素个数,最大.次大.最小.次小 # ...

  7. ngx_lua_API 指令详解(五)coroutine.create,coroutine.resume,coroutine.yield 等集合指令介绍

    ngx_lua 模块(原理实现) 1.每个worker(工作进程)创建一个Lua VM,worker内所有协程共享VM: 2.将Nginx I/O原语封装后注入 Lua VM,允许Lua代码直接访问: ...

  8. js拾遗: replace 替换参数

    先来看一个简单的例子吧. var str = "123X321".replace("X", "$'"); 替换后的 str 是什么呢?是 & ...

  9. js截取图片上传(仅原理)----闲的无聊了代码就不共享了!写的难看,不好意思给你们看了(囧)

    就算世界再坑爹,总有一些属性能带你走出绝望(伟大的absolute) 今天吐槽一下!......在我的世界里没有正统UI,所以效果图永远都是那么坑爹! 这里我要感谢有个position:absolut ...

  10. html5 canvas 奇怪的形状水平渐变(因为大多数的之前的文章把基础都打过了,所以开始写的快了,如果有不明白的,可以回顾下之前的)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...