小编Jef*_*son的帖子

RxJS:takeUntil()角度组件的ngOnDestroy()

tl; dr:基本上我想将Angular ngOnDestroy与Rxjs takeUntil()运算符结合起来. - 那可能吗?

我有一个Angular组件,可以打开几个Rxjs订阅.当组件被销毁时,需要关闭它们.

一个简单的解决方案是:

class myComponent {

  private subscriptionA;
  private subscriptionB;
  private subscriptionC;

  constructor(
    private serviceA: ServiceA,
    private serviceB: ServiceB,
    private serviceC: ServiceC) {}

  ngOnInit() {
    this.subscriptionA = this.serviceA.subscribe(...);
    this.subscriptionB = this.serviceB.subscribe(...);
    this.subscriptionC = this.serviceC.subscribe(...);
  }

  ngOnDestroy() {
    this.subscriptionA.unsubscribe();
    this.subscriptionB.unsubscribe();
    this.subscriptionC.unsubscribe();
  }

}
Run Code Online (Sandbox Code Playgroud)

这有效,但有点多余.我特别不喜欢那样 - 这unsubscribe()是其他地方,所以你必须记住这些是相互关联的. - 组件状态受订阅污染.

我更喜欢使用takeUntil()运算符或类似的东西,使它看起来像这样:

class myComponent {

  constructor(
    private serviceA: ServiceA,
    private serviceB: ServiceB,
    private serviceC: ServiceC) {}

  ngOnInit() {
    const destroy = Observable.fromEvent(???).first();
    this.subscriptionA …
Run Code Online (Sandbox Code Playgroud)

components rxjs rxjs5 angular

32
推荐指数
5
解决办法
2万
查看次数

如何验证列表是否已排序?

我怎么能在Clojure中验证是否对数字列表进行了排序?

(def my-list (list 1 2 3 1 4 2 2 4))
Run Code Online (Sandbox Code Playgroud)

sorted?仅当集合实现sorted接口时才返回true .我正在寻找一种reduce可以成对迭代列表的操作,例如(reduce < my-list).我知道我可以手动创建对并比较这些:

(letfn [(pair [l] (if (= (count l) 2) (list l) (cons (take 2 l) (pair (rest l)))))]
    (every? #(apply < %) (pair my-list)))
Run Code Online (Sandbox Code Playgroud)

但这似乎不必要地复杂.在我看来,好像我错过了一个基本功能.

list sorted clojure

8
推荐指数
2
解决办法
985
查看次数

找不到带有Webpack,Typescript,自定义模块目录的模块

我想做什么

我写了一个虚拟模块my-component,它基本上导出了一个单独的Something类.我把它放在app/modules /中.现在我想用app/app.js中的导入语法来访问它:

import { Something } from 'my-component';
Run Code Online (Sandbox Code Playgroud)

期望:使用我当前的Webpack配置(下面),我希望这可以工作.

实际:这会抛出错误:

ERROR in [default] /<project_dir>/app/app.ts:1:26
Cannot find module 'my-component/Something'.
Run Code Online (Sandbox Code Playgroud)

我试图解决它

我知道模块本身是正确定义的,因为

  1. 我可以使用相对路径导入它: import { Something } from './my-component'
  2. 如果我将模块移动到,我可以原样导入它node_modules/my-component.

唯一失败的组合是导入它而没​​有来自我的modules /目录的相对路径.所以我认为问题可能是我的Webpack配置.

安装细节

如下所示,我有两个目录列为resolve.root:

  • project_dir/app
  • project_dir/node_modules

它似乎设法解决node_modules,而不是来自app.

项目布局

                               Webpack
project_dir/
 ??? app/                      context, resolve.root
 ?    ??? app.ts
 ?    ??? my-component/
 ?         ??? index.ts
 ?         ??? Something.ts
 ??? webpack.config.js
 ??? node_modules/             resolve.root …
Run Code Online (Sandbox Code Playgroud)

javascript import module typescript webpack

6
推荐指数
2
解决办法
8005
查看次数

无法对泛型子类(Java)进行静态引用

我有以下代码:

class SuperClass {
    public static String getName() { return "super"; }
}

class SubClass extends SuperClass {
    public static String getName() { return "sub"; }
}

public class Dummy<T extends SuperClass> {
    public void print() {
        System.out.println("SuperClass: " + SuperClass.getName());
        System.out.println("SubClass: " + SubClass.getName());
        System.out.println("T: " + T.getName());
    }
    public static void main(String[] args) {
        new Dummy<SubClass>().print();
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码输出以下内容:

SuperClass: super
SubClass: sub
T: super
Run Code Online (Sandbox Code Playgroud)

我的问题是:为什么T.getName()不返回SubClass.getName()的值?毕竟,我指定了T == SubClass.或者静态函数调用对于泛型引用是否无效?

非常感谢提前!

java generics inheritance static extends

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

如何使用Latex和scrbook包对齐页脚右侧的页码?

我正在使用scrbook包编写Latex文档.

\documentclass[12pt,a4paper,oneside]{scrbook}
\pagestyle{plain}
Run Code Online (Sandbox Code Playgroud)

页码始终显示在页脚的中心.如何让它在右边对齐?我试过几次找到它的文档中,但说实话,我不完全理解,我的截止时间就是现在.

latex alignment

3
推荐指数
1
解决办法
6958
查看次数