在下面的代码片段中,我有一个TypeScript类,而实例方法buz是canvas' click事件的监听器.
thisbuz方法中的关键字是指事件的目标对象(画布).
如何foo从buz方法访问实例?
class Foo {
constructor(private _canvas: HTMLCanvasElement, private _message: string) {
}
public bar(): void {
this._canvas.addEventListener(`click`, this.buz);
}
private buz(e: MouseEvent): void {
console.info(`After click event, 'this' refers to canvas and not to the instance of Foo:`);
console.info(this);
console.warn(`Message is: "${this._message}"`); // error
}
}
window.addEventListener('load', () => {
let canvas = <HTMLCanvasElement> document.getElementById('canvas');
let foo = new Foo(canvas, "Hello World");
foo.bar();
});
Run Code Online (Sandbox Code Playgroud)
我tsconfig.json …
是否有任何内置功能Newtonsoft.Json可以将枚举值序列化为蛇形名称?
目前,我手动提供值:
[JsonConverter(typeof(StringEnumConverter))]
enum MyEnum {
[EnumMember(Value = "value_one")]
ValueOne,
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个接受输入类型的突变FooInputType。我的问题是在该输入上有一个字段来表示 JSON 字典(字符串-字符串)。例如,foo.tags字段可以有任何一组字符串类型的键值对。
样本突变:
{
"query": "mutation ($foo: FooInputType) { addFoo(foo: $foo) { tags } }",
"variables": {
"foo": { "bar": "test", "tags": { "priority": "high" } }
}
}
Run Code Online (Sandbox Code Playgroud)
这是突变:
class MyMutation : ObjectGraphType {
public MyMutation() {
Field<TaskItemType>("addFoo", "Add a new foo",
new QueryArgument<FooInputType> {Name = "foo"}
);
}
}
Run Code Online (Sandbox Code Playgroud)
和输入类型:
class FooInputType : InputObjectGraphType {
public FooInputType() {
Field<StringGraphType>("bar");
Field<InputObjectGraphType<JObject>>("tags");
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Blazor(客户端)构建一个简单的Web应用程序,需要让Blazor重新渲染该组件。有没有办法通知框架重新渲染?
在此Razor页面上,我有一个简单的复选框,显示用户是否向Web应用授予了一定的权限。
ShouldRender()总是返回True。当isGrantedfield设置True为时,该复选框不会呈现,并且保持未选中状态。
剃刀页面:
@page "/foo"
@inject IJSRuntime js
<input type="checkbox" disabled @bind="isGranted" /> Some Permission
@code {
bool isGranted;
protected override async Task OnInitAsync() {
await js.InvokeAsync<object>(
"foo.bar",
DotNetObjectRef.Create(this),
nameof(BarCallback)
);
}
[JSInvokable]
public void BarCallback(bool result) {
Console.WriteLine($"BarCallback(result: {result})");
isGranted = result;
}
protected override bool ShouldRender() {
Console.WriteLine("Blazor is checking for re-rendering...");
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
JavaScript函数:
window.foo = {
bar: (dotnetObjRef, callback) => {
navigator.storage.persist()
.then(result => {
dotnetObjRef.invokeMethodAsync(callback, …Run Code Online (Sandbox Code Playgroud)