相关疑难解决方法(0)

如何实现打字稿装饰器?

TypeScript 1.5现在有装饰器.

有人可以提供一个简单的例子来演示实现装饰器的正确方法,并描述可能的有效装饰器签名中的参数是什么意思吗?

declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;
declare type PropertyDecorator = (target: Object, propertyKey: string | symbol) => void;
declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
declare type ParameterDecorator = (target: Function, propertyKey: string | symbol, parameterIndex: number) => void;
Run Code Online (Sandbox Code Playgroud)

另外,在实现装饰器时是否应该记住哪些最佳实践注意事项?

decorator typescript

198
推荐指数
3
解决办法
7万
查看次数

如何在Typescript中编译装饰器(注释)?

在Angular 2中,我可以创建一个组件,如下所示:

import {Component, Template} from 'angular2/angular2'

@Component({
  selector: 'my-component'
})
@View({
  inline: "<div>Hello my name is {{name}}</div>"
})
export class MyComponent {
  constructor() {
    this.name = 'Max'
  }
  sayMyName() {
    console.log('My name is', this.name)
  }
}
Run Code Online (Sandbox Code Playgroud)

(来源:http://blog.ionic.io/angular-2-series-components/)

然后将其编译为常规ES5.

我的问题分为两部分:

  1. 这些装饰器特定于Angular.他们是如何定义的?
  2. 如何定义自己的装饰器?

typescript angular

11
推荐指数
1
解决办法
6863
查看次数

标签 统计

typescript ×2

angular ×1

decorator ×1