我创建了一个类装饰器来读取 Angular 组件的权限。
装饰器.ts
function permissions(object: { [key: 'read' | 'write' | 'update' | 'delete']: string }[]) {
return (ctor: Function) => {
console.log(JSON.stringify(object));
}
}
组件.ts
// employee.component.ts
@permissions([{ read: 'read-unique-id' }])
@Component({
selector: 'app-employee',
templateUrl: './app-employee.component.html',
styleUrls: ['./app-employee.component.css']
})
class Employee {
constructor() { }
}
// employee-details.component.ts
@permissions([{ read: 'read-unique-id' }, { write: 'write-unique-id' }])
@Component({
selector: 'app-employee-details',
templateUrl: './app-employee-details.component.html',
styleUrls: ['./app-employee-details.component.css']
})
class EmployeeDetails {
constructor() { }
}
我应该通过在编译期间从装饰器读取类名以及权限详细信息来创建一个文件。只需一个命令即可生成以下文件。
示例:Permissions.ts
class Permissions {
static EMPLOYEE_READ = 'read-unique-id';
static EMPLOYEE_DETAILS_READ = 'read-unique-id';
static EMPLOYEE_DETAILS_WRITE = 'write-unique-id';
}
我相信我应该创建一个 Node JS 应用程序来读取所有文件并检查 @permissions 装饰器并生成上述文件。这是正确的吗?
如果是这样,有人可以帮助我实现这一目标吗?读取所有的component.ts文件并获取权限详细信息或者是否有任何其他方法可以实现相同的目的。
Update: I should read permission details from component.ts through decorator and generate Permissions.ts with help of Node JS app.
最佳答案
一种更简单且类型安全的方法是朝相反方向移动并使用一组常量而不是字符串,例如与枚举
:
enum Permissions {
EMPLOYEE_READ = 'read-unique-id';
...
}
...
@permissions([{ read: Permissions.EMPLOYEE_READ }, ...}])
包含常量的模块也可以在后端应用程序中重用。
关于javascript - typescript:读取 Node js 中的类装饰器文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50785608/