我看到babel.js装饰器(在"第1阶段"中可用)在https://github.com/wycats/javascript-decorators上实现了规范.看起来装饰器仅限于(1)类,(2)访问器和(3)方法.在我的例子中,我想在普通的旧函数上使用装饰器,如
@chainable
function foo() { }
Run Code Online (Sandbox Code Playgroud)
在哪里(只是一个例子)
function chainable(fn) {
return function() {
fn.apply(this, arguments);
return this;
};
}
Run Code Online (Sandbox Code Playgroud)
我没有看到装饰器不能应用于函数的任何逻辑原因.我的问题是,有没有办法实现这一目标?还是有一些很好的理由为什么功能无法装饰?
事实证明,在https://github.com/wycats/javascript-decorators/issues/4上存在一个问题.
javascript decorator babeljs ecmascript-next javascript-decorators
我看到装饰器今天已经在一些javascript代码中使用了.我的问题实际上是双重的.
第一:
如果装饰者还没有最终确定如何在生产代码中使用它们呢?浏览器支持不会不存在吗?
第二:
鉴于今天有可能使用它,正如一些开源项目所暗示的那样,通常建议的设置是什么让装饰器工作?
javascript angularjs-decorator ecmascript-next javascript-decorators
我试图在运行时动态创建 Typeorm 实体,然后在创建连接时将它们显式添加到连接中。我在弄清楚如何使用属性装饰器时遇到了一些麻烦。
作为控件,以声明方式创建实体效果很好:
@Entity('table_name')
export class NewEntity extends BaseEntity {
@PrimaryGeneratedColumn()
public id: number;
}
Run Code Online (Sandbox Code Playgroud)
然后类装饰器可以像常规函数一样正常工作:
class NewEntity {
@PrimaryGeneratedColumn()
public id: number;
}
Entity('table_name')(NewEntity);
Run Code Online (Sandbox Code Playgroud)
但PrimaryGeneratedColumn()作为常规函数使用:
class NewEntity {
public id: number;
}
PrimaryGeneratedColumn()(NewEntity, 'id');
Entity('table_name')(NewEntity);
Run Code Online (Sandbox Code Playgroud)
给出:Entity "NewEntity" does not have a primary column. Primary column is required to have in all your entities. Use @PrimaryColumn decorator to add a primary column to your entity.创建 typeorm 连接时。
我知道这是一个奇怪的用例!但如果有人知道如何做到这一点,将不胜感激!:)
嗨,如果有人愿意帮助我,我想在我的 nextjs 应用程序上使用 @Decorators,但出现以下错误。
希望有人知道我做错了什么
错误信息:
Syntax error: Support for the experimental syntax 'decorators-legacy' isn't currently enabled:
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 typeorm / type-graphql 在应用程序子文件夹中生成我的后端。
应用程序/预订/模型/booking.ts
Syntax error: Support for the experimental syntax 'decorators-legacy' isn't currently enabled:
Run Code Online (Sandbox Code Playgroud)
巴别尔
{
"presets": [
[
"next/babel",
{
"plugin-proposal-decorators": {"legacy": true}
}
]
],
}
Run Code Online (Sandbox Code Playgroud)
下一个.config.js
import {
Field,
ID,
ObjectType,
} from 'type-graphql';
import {
BaseEntity,
ManyToOne,
Column,
CreateDateColumn,
Entity,
PrimaryGeneratedColumn,
UpdateDateColumn,
VersionColumn,
} from 'typeorm';
import { ArrayNotEmpty, ArrayMinSize, IsPositive, Min, IsNotEmpty, ValidateNested, MaxLength, Length, …Run Code Online (Sandbox Code Playgroud) 我将 InversifyJS 与绑定和惰性注入装饰器一起使用,并且收到下一个错误:
找不到 serviceIdentifier: Symbol(ClassB) 的匹配绑定
这是代码:
inversify.config.ts
import { Container } from 'inversify';
import getDecorators from 'inversify-inject-decorators';
import { buildProviderModule, provide } from 'inversify-binding-decorators';
import Types from './types';
const container = new Container();
container.bind<MyType>(Types.MyType)
.to(MyType);
const { lazyInject } = getDecorators(container, false);
container.load(buildProviderModule());
export { container, lazyInject, provide };
Run Code Online (Sandbox Code Playgroud)
MyType.ts
import { inject, injectable } from 'inversify';
@injectable()
export class MyType{
constructor(
@inject(Q) private readonly Q: Q,
@inject(W) private readonly W: W,
@inject(E) private readonly E: E) { …Run Code Online (Sandbox Code Playgroud) typescript javascript-import inversifyjs javascript-decorators
我正在尝试使用终端在 NodeJS 中运行以下代码
function addStringToName(target, name, descriptor) {
const fn = descriptor.value;
descriptor.value = wrestler => {
fn.call(target, wrestler + ' is a wrestler');
};
}
class Wrestler {
@addStringToName
setName(name) {
this.name = name;
}
sayName() {
console.log(this.name);
}
}
const w = new Wrestler();
w.setName('Macho Man');
w.sayName();
Run Code Online (Sandbox Code Playgroud)
出现以下错误
NodeJS 中可以使用装饰器吗?如果可以的话,编写的代码有什么问题吗?
用例:我想知道一个函数在打字稿中执行需要多长时间。我想使用装饰器来达到这个目的。我希望装饰器应该返回时间,以便(我可以进一步使用它),而不仅仅是打印它。
例如:
export function createTimestamps(message: string) {
return function (target: any, name: string, descriptor: PropertyDescriptor) {
const method = descriptor.value;
descriptor.value = async function () {
const startTime = new Date(Date.now());
console.log(
`${message} started at: ${startTime.toLocaleString("en-GB")}`
);
await method.apply(this);
const endTime = new Date(Date.now());
console.log(
`${message} completed at: ${endTime.toLocaleString("en-GB")}`
);
console.log(
`${message} took ${
endTime.getTime() - startTime.getTime()
}ms to complete.`
);
};
};
}
Run Code Online (Sandbox Code Playgroud)
如果我使用上面的函数作为装饰器,那么我希望装饰器返回“endTime.getTime() - startTime.getTime()”,以便我可以进一步使用它。
@creaTimestamp
async newfunc():string{
return "typescript";
}
Run Code Online (Sandbox Code Playgroud)
现在,当我调用上面的函数时,await newfunc()。我可以获取执行时间值以及它返回的字符串吗?
另外,我有很多函数,我想避免在每个函数之上添加装饰器,因此在调用它们时,我想确保装饰器运行并返回计时。有人可以指出我这样的图书馆(如果存在)吗?
有人可以分享一些对上述场景的见解吗,我对装饰器很陌生。谢谢!
javascript ×3
babeljs ×2
decorator ×2
typescript ×2
inversifyjs ×1
next.js ×1
node.js ×1
typeorm ×1