我想使用 TypeScript Decorator 创建一个属性 getter 和 setter,但我一直坚持定义在属性更改或请求时运行的函数。
如果我的装饰器有以下用法Field.watch:
export class Test {
...
@Field.watch({onSet: this.resetCssCache})
public layoutId = 0;
...
public resetCssCache() {
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
装饰器的实现是:
export class Field {
public static watch(watchers: { onGet?: () => any, onSet?: (newValue: any, oldValue: any) => any }): Function {
return (target: any, key: string) => {
// property getter
const getter = function() {
return this['_' + key];
};
// property setter
const setter = function(newVal) { …Run Code Online (Sandbox Code Playgroud) 我有一个数据存储并想创建一种方法来从存储加载数据。存储包含不同类型(类)的数据。假设我的商店包含(除其他外)作者类型的数据。我想加载 id 为 1 的作者:
const author1 = store.loadById(Author, 1);
Run Code Online (Sandbox Code Playgroud)
现在我如何使用泛型让 TS 编译器知道 author1 是 Author 的一个实例?
我现在有
public loadById<T>(entityClass: T, id: number): T {
const entity;
// logic to load the entity ...
return entity;
}
Run Code Online (Sandbox Code Playgroud)
但这是错误的,因为现在 TSC 认为我的方法返回一个 entityClass 而不是 entityClass 的一个实例。那么我如何指定方法的返回类型才能让 author1 成为 Author 的一个实例呢?
在 OpenAPI 3 中,是否可以在全局级别定义 SecurityScheme,然后在某些端点覆盖它以不使用安全性(对于公共可访问端点)?
例如(取自https://swagger.io/docs/specification/authentication/bearer-authentication/)
openapi: 3.0.0
...
# 1) Define the security scheme type (HTTP bearer)
components:
securitySchemes:
bearerAuth: # arbitrary name for the security scheme
type: http
scheme: bearer
bearerFormat: JWT # optional, arbitrary value for documentation purposes
# 2) Apply the security globally to all operations
security:
- bearerAuth: [] # use the same name as above
Run Code Online (Sandbox Code Playgroud)
然后使给定端点可公开访问(不受保护)
paths:
/unprotected/path:
get:
security: []
Run Code Online (Sandbox Code Playgroud)
或者应该以另一种方式完成?
更新这个问题被标记为重复,但另一个问题处理有关 Swagger 2.x 的问题,并且由于语法不同,我认为这个问题和答案应该保留。
我试图使用Angular(2+)渲染一些SVG文本.
在调试器中,我看到svg树中的文本节点,但文本在屏幕上不可见.
我创建了一个plunkr来演示这个问题:http://plnkr.co/edit/QCc39AiDzxSeQMGiDqQC?p = preview
应用:
import {Component} from 'angular2/core';
import {SvgTextComponent} from 'src/svgtext.component.ts';
@Component({
selector: 'my-app',
providers: [],
template: `
<div>
<h2>Hello {{name}}</h2>
<svg style="border: solid 1px dimgray; width: 100%; height: 600px;">
<g svg-text></g>
</svg>
</div>
`,
directives: [SvgTextComponent]
})
export class App {
constructor() {
this.name = 'Angular2'
}
}
Run Code Online (Sandbox Code Playgroud)
svgtext:
import {Component} from "angular2/core";
@Component({
selector: 'g[svg-text]',
template:'<text x="150" y="100">Hello world from Ng2!</text>',
})
export class SvgTextComponent { }
Run Code Online (Sandbox Code Playgroud)
当我查看DOM树时,文本节点就在那里......只是没有显示.如果我只是直接在App组件的模板中复制文本节点,则文本显示...
我怎样才能解决这个问题?
谢谢!
Google正在开发AtScript,如果我是对的,它将与Angular 2.0一起推出.现在,我想知道我们是否能够将我们的TypeScript文件迁移到AtScript并继续使用这种新语法.
O,有足够声誉的人可以创建标记AtScript吗?谢谢!
我如何将值存储到变量中,例如在单击角时?我有以下代码无法将值保存到变量中:
HTML:
<div ng-app='app'>
<a ng-controller="MyController" href="#" ng-click="variable = 1">
</a>
{{variable}}
</div>
Run Code Online (Sandbox Code Playgroud)
控制器:
var app = angular.module('app',[]);
app.controller('MyController', function($scope)
{
});
Run Code Online (Sandbox Code Playgroud)
我想创建一个可以选择接受参数的字段装饰器。参数应包含以下任何值:无、布尔值或函数。我知道怎么做,但我对结果不是 100% 满意:
export class TestClass{
@Required(isRequired)
public testField: string;
}
export function isRequired():boolean{
... some validation logic, maybe depending on other fields...
return result;
}
Run Code Online (Sandbox Code Playgroud)
@Required 的实现:
export function Required(expression?: boolean|Function): Function {
return (target: any, key: string) => {
if (expression === null || typeof expression == 'undefined') {
expression = true;
}
console.log("Required found: " + expression, ":", target, key);
... register the field and its validation expression for later usage
}
}
Run Code Online (Sandbox Code Playgroud)
所以这很好用,但是当我不想添加表达式(因此使用默认的“true”表达式)时,我希望能够像这样写:
class TestClass{
@Required …Run Code Online (Sandbox Code Playgroud)