你能描述一下TypeScript语言是什么吗?
JavaScript或可用库无法做什么,这会让我有理由考虑它?
我正在使用typescript进行角度2应用程序开发.
但是当我们为组件或路由配置或其他地方编写代码时,我们使用"@"符号.
我的问题是这个符号的含义以及为什么需要它?
我正在使用TypeScript来定义一些类,当我创建一个属性时,它会生成Class1以下plunkr中的等价物:
http://plnkr.co/edit/NXUo7zjJZaUuyv54TD9i?p=preview
var Class1 = function () {
this._name = "test1";
}
Object.defineProperty(Class1.prototype, "Name", {
get: function() { return this._name; },
set: function(value) { this._name = value; },
enumerable: true
});
JSON.stringify(new Class1()); // Will be "{"_name":"test1"}"
Run Code Online (Sandbox Code Playgroud)
序列化时,它不会输出我刚刚定义的属性.
instance2并且instance3通过序列化已定义的属性来表现我的期望.(参见plunkr输出).
我的实际问题是:这是正常的吗?
如果是这样,我该如何以最有效的方式解决它?
如何实现打字稿装饰器?关于如何在打字稿中使用装饰器是一个很好的例子.
考虑到以下情况,
class MyClass {
@enumerable(false)
get prop() {
return true;
}
@property({required: true}) //here pass constant is no issue
public startDateString:string;
@property({afterDate: this.startDateString}) //how to pass startDateString here?
public endDateString:string;
}
function enumerable(isEnumerable: boolean) {
return (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => {
descriptor.enumerable = isEnumerable;
return descriptor;
};
}
Run Code Online (Sandbox Code Playgroud)
我尝试了一切,但似乎我无法startDateString进入装饰器参数.startDateString可以是变量,函数和引用.
在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.
我的问题分为两部分:
你知道是否可以使用装饰器获取类实现的接口数组:
interface IWarrior {
// ...
}
interface INinja {
// ...
}
Run Code Online (Sandbox Code Playgroud)
所以,如果我这样做:
@somedecorator
class Ninja implements INinja, IWarrior {
// ...
}
Run Code Online (Sandbox Code Playgroud)
在运行时Ninja将有一个注释,其中包含["INinja", "IWarrior"]?
谢谢
有没有办法使用Angular的DI将依赖项注入装饰器工厂?我们将以下代码作为简化示例:
@Component({
selector: 'hello-component',
template: '<div>Hello World</div>'
})
export class HelloComponent {
@PersonName()
name: string;
ngAfterViewInit() {
console.log(`Hello, ${this.name}`);
}
}
Run Code Online (Sandbox Code Playgroud)
这里,PersonName装饰器的预期行为是它访问Person依赖项,并使用它来设置name类的属性.
是否有可能PersonName为上面的代码实现装饰器?
我读过"如何实现打字稿装饰器?" 和多个来源,但我有一些东西,也没有能够与装饰.
class FooBar {
public foo(arg): void {
console.log(this);
this.bar(arg);
}
private bar(arg) : void {
console.log(this, "bar", arg);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我们调用函数foo:
var foobar = new FooBar();
foobar.foo("test");
Run Code Online (Sandbox Code Playgroud)
该对象FooBar由console.log(this);in 登录到控制台中foo
该字符串"FooBar {foo: function, bar: function} bar test"由控制台记录在控制台console.log(this, "bar", arg);中bar.
现在让我们使用装饰器:
function log(target: Function, key: string, value: any) {
return {
value: (...args: any[]) => {
var a = args.map(a => JSON.stringify(a)).join();
var result = value.value.apply(this, …Run Code Online (Sandbox Code Playgroud) 我一直认为TypeScript中的装饰器是在类的构造函数之后调用的.但是,我被告知,例如,这篇文章的最高答案声称在声明类时调用Decorator,而不是在实例化对象时调用.我注册的Angular课程的Udemy讲师也告诉我,Typescript中的装饰器在属性初始化之前运行.
但是,我在这个问题上的实验似乎表明不是这样.例如,这是一个带有属性绑定的简单Angular代码:
test.component.ts
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-test',
template: '{{testString}}'
})
export class TestComponent{
@Input() testString:string ="default string";
constructor() {
console.log(this.testString);
}
}
Run Code Online (Sandbox Code Playgroud)
app.component.html
<app-test testString="altered string"></app-test>
Run Code Online (Sandbox Code Playgroud)
当我执行代码时,控制台记录"默认字符串"而不是"更改字符串".这证明了在类的构造函数执行后调用decorator.
有人可以给我一个明确的答案,说明何时调用装饰器?因为我在网上的研究与我做的实验相矛盾.谢谢!
typescript ×10
angular ×4
decorator ×4
javascript ×4
ecmascript-7 ×2
json ×2
aurelia ×1
babeljs ×1