小编dis*_*nte的帖子

JavaScript-返回两个对象之间的差异?

有人可以告诉我如何在比较这种情况时返回新数据。使用香草JavaScript。

{
    "48": "{\"sid\":\"48\",\"name\":\"title 1\"}",
    "77": "{\"sid\":\"77\",\"name\":\"The blahblah title\"}"
}
Run Code Online (Sandbox Code Playgroud)

与此相比

{
    "48": "{\"sid\":\"48\",\"name\":\"title 1\"}",
    "77": "{\"sid\":\"77\",\"name\":\"The blahblah title\"}",
    "83": "{\"sid\":\"83\",\"name\":\"The blahblah derp\"}",
    "87": "{\"sid\":\"87\",\"name\":\"The derpy title 4\"}"
}
Run Code Online (Sandbox Code Playgroud)

它应该只返回差异。

{
    "83": "{\"sid\":\"83\",\"name\":\"The blahblah derp\"}",
    "87": "{\"sid\":\"87\",\"name\":\"The derpy title 4\"}"
}
Run Code Online (Sandbox Code Playgroud)

javascript object

8
推荐指数
3
解决办法
152
查看次数

在 Angular 中开玩笑测试后如何自动调用discardPeriodicTasks?

我做了一个一次性订阅,debounceTime在我的组件上有一个管道onInit

this.subscriptions.add(
  this.updateJobs$
    .pipe(
      filter(jobs => !!jobs.length),
      debounceTime(MyComponent.ListStabilizationTimeInMS),
      first(),
    )
    .subscribe((jobs: Update[]) => {
      // some code
    })
);
Run Code Online (Sandbox Code Playgroud)

因此,我需要discardPeriodicTasks();在每次fakeAsync测试后将其作为最后一行包含在内,我觉得这很乏味。否则我会收到Error: 1 periodic timer(s) still in the queue.所有测试的错误。

我试图创建一个afterEach

afterEach(()=> {
  discardPeriodicTasks();
});

afterEach(fakeAsync(()=> {
  discardPeriodicTasks();
}));
Run Code Online (Sandbox Code Playgroud)

但它不起作用,通过测试的唯一方法是手动附加discardPeriodicTasks();到所有测试用例。如果可以自动化就太好了。

jestjs angular

8
推荐指数
0
解决办法
1347
查看次数

停止后是否需要断开振荡器AudioNode?

我在人们使用振荡器节点.disconnect()后使用的几个教程中看到过.stop().

据我所知,振荡器节点是一次性的,所以当它停止并被扔掉时,它是否也会断开连接?

我理解错了吗?为什么我需要在使用.disconnect()后使用.stop()

javascript web-audio-api

7
推荐指数
2
解决办法
410
查看次数

如何在没有*ngIf的情况下渲染ng-container或ng-template?

我需要将tappable指令添加到ion-card自定义组件内的组件.我用的@Input() myInputBool是:

<ng-container *ngIf="myInputBool">
    <ion-card tappable>
        <ng-container render="myContent"></ng-container>
    </ion-card>
</ng-container>

<ng-container *ngIf="!myInputBool">
    <ion-card tappable>
        <ng-container render="myContent"></ng-container>
    </ion-card>
</ng-container>

<ng-container #myContent>
    This is my content
</ng-container>
Run Code Online (Sandbox Code Playgroud)

当然它不起作用,因为没有"渲染"选项.到目前为止,我的解决方法是在ng-container中添加一个不存在的变量

<ng-container *ngIf="thisVariableDoesNotExist else myContent"> </ng-container>
Run Code Online (Sandbox Code Playgroud)

但它感觉很糟糕和黑客.有更好的方法吗?

ionic3 angular

7
推荐指数
1
解决办法
4000
查看次数

如何防止vscode的“格式文档”删除打字稿和scss文件上的空行?

我使用Ionic,某些scss文件具有以下格式:

// Ionic Variables and Theming. For more info, please see:
// http://ionicframework.com/docs/v2/theming/
$font-path: "../assets/fonts";

@import "ionic.globals";


// Shared Variables
// --------------------------------------------------
// To customize the look and feel of this app, you can override
// the Sass variables found in Ionic's source scss files.
// To view all the possible Ionic variables, see:
// http://ionicframework.com/docs/v2/theming/overriding-ionic-variables/




// Named Color Variables
// --------------------------------------------------
// Named colors makes it easy to reuse colors on various components.
// It's highly recommended …
Run Code Online (Sandbox Code Playgroud)

visual-studio-code vscode-settings

6
推荐指数
0
解决办法
734
查看次数

如何在接口中声明可选的泛型类型?

我有这个界面:

export interface AlertInteraction<T> {
  id: AlertId;
  data: T;
  buttonId: AlertButtonId;
}
Run Code Online (Sandbox Code Playgroud)

但有时不需要数据。我知道我可以data: T将它声明为,但我想知道我是否可以这样:

export interface AlertInteraction<T> {
  id: AlertId;
  data: T;
  buttonId: AlertButtonId;
}

export interface AlertInteraction {
  id: AlertId;
  buttonId: AlertButtonId;
}

Run Code Online (Sandbox Code Playgroud)

所以,如果我给,T那么我假设我想访问数据,如果没有,那么假设它不存在。是否可以?

typescript typescript-generics

6
推荐指数
1
解决办法
2200
查看次数

如何在 Typescript 中定义通用类型防护?

我想为我的类定义一个接口,其中包含一个isValidConfig用作类型保护的函数。但我不知道如何声明。

我已经这样做了:

type AnyConfig = ConfigA | ConfigB | ConfigC;

public abstract isValidConfig<T extends AnyConfig>(config: AnyConfig): config is T;

Run Code Online (Sandbox Code Playgroud)

  public abstract isValidConfig<T = AnyConfig>(config: T): config is T;
Run Code Online (Sandbox Code Playgroud)

但我在实现中总是遇到错误,例如:

public isValidConfig<T extends ConfigA >(config: T): config is T {
    return config.type === TrainingTypes.A;
} /// Types of parameters 'config' and 'config' are incompatible.
      Type 'T' is not assignable to type 'ConfigA '.
Run Code Online (Sandbox Code Playgroud)

是否有可能做到这一点?我还没有找到路。

typescript angular

6
推荐指数
1
解决办法
5878
查看次数

如何创建一个条件泛型类型,该类型在未定义时有值,但在未定义时没有值以及其他情况

我有一种类型,我想将对象键定义为可选,any如果我不给出任何值

type OptionalData<T> = T extends undefined ? { data?: any } : { data: T };

export type DataObject<T = any> = {
  item?: string | null;
} & OptionalData<T>;

Run Code Online (Sandbox Code Playgroud)

它工作正常,直到我将我的通用设置为something | undefined

const data: DataObject<string | undefined> = { data: true }; // WRONG!
const data2: DataObject<string | number> = { data: true }; // CORRECT
Run Code Online (Sandbox Code Playgroud)

因为我的通用也可以扩展undefined我获取数据作为可选any

ipt

有没有办法说“any当它可以扩展时它只是可选的undefined”或者这样?

操场

typescript typescript-generics

6
推荐指数
1
解决办法
2840
查看次数

到底什么时候需要compileComponents?

我正在将仅使用 jest 的组件单元测试迁移到使用 Testbed 和 jest 的 UI 测试(在我仅使用 jest 进行单元测试之前component = new MyCompontent())。

我正在使用 Angular 11。

有件事我不明白compileComponents。在文档中,它说“如果您只使用 CLI ng test 命令运行测试,则可以忽略此部分,因为 CLI 在运行测试之前编译应用程序。”

我没有打电话ng test,但jest --watch我的测试无论有没有都有效compileComponents(我所说的工作是指它们正确地断言了我的观点)。

还有一个“如果任何测试模块组件具有 templateUrl 或 styleUrls,则必须调用此方法,因为获取组件模板和样式文件必然是异步的”

我的组件同时使用templateUrlstyleUrls

@Component({
  selector: 'app-my-example',
  templateUrl: './my-example.component.html',
  styleUrls: ['./my-example.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MyExampleComponent {}
Run Code Online (Sandbox Code Playgroud)

那么我缺少什么?我想确保如果我最终设置 CI 或类似的内容,我的测试不会中断。

或者我应该总是打电话compileComponents?我的印象是,如果不需要的话,这样做不会那么有效。

那么,具体什么时候compileComponents需要呢?

编辑:

这是我的笑话配置

module.exports = {
  preset: 'jest-preset-angular',
  setupFilesAfterEnv: ['<rootDir>/src/setupJest.ts'],
  transformIgnorePatterns: …
Run Code Online (Sandbox Code Playgroud)

testbed jestjs angular

6
推荐指数
1
解决办法
1157
查看次数

使用 Java 中的 switch 比较字符串和枚举值

注意:这不是Java switch 语句的重复:需要常量表达式,但它是常量,因为该链接的解决方案已在此处应用。


在带有打字稿的 Cordova 应用程序中,我使用此枚举来发送我的操作=

打字稿

enum PluginActions {
   PICK = "PICK",
   PICK_MULTIPLE = "PICK_MULTIPLE"
}
Run Code Online (Sandbox Code Playgroud)

我将其发送给 cordova,并在 Java 中将其作为我的方法中的字符串变量获取action

@Override
  public boolean execute(String action, JSONArray inputs, CallbackContext callbackContext) throws JSONException {

  }

Run Code Online (Sandbox Code Playgroud)

那里我还有一个枚举。

爪哇

enum PickerActions {
  PICK, PICK_MULTIPLE
}
Run Code Online (Sandbox Code Playgroud)

我想比较 typescriptPluginActions和 java PickerActions

使用if我可以使用:

if (action.equals(PickerActions.PICK.name())) { }

但我想用一个开关来做到这一点,这样我就可以轻松添加更多操作

  switch (action) {
    case PickerActions.PICK.name():
      JSONObject filters = inputs.optJSONObject(0);
      this.chooseFile(filters, callbackContext);
      return true;
    default:
    Log.w(this.LOGGER_TAG, "No function was found …
Run Code Online (Sandbox Code Playgroud)

java enums

5
推荐指数
1
解决办法
4026
查看次数