ng2-dragula setOptions 和删除版本问题

ng2-dragula setOptions and drop version problem

显然 1.5.0 支持 this.dragulaService.setOptions,而 2.1.1 不支持,相反,2.1.1 支持 this.dragulaService.drop 订阅 1.5.0 不支持。

Stackblitz Fork for 1.5.0

Stackblitz Fork for 2.1.1

需要注意的相关代码:

1.5.0(不工作)

(Error):

Cannot invoke an expression whose type lacks a call signature. Type 'EvenEmitter' has no compatible call signatures. (property) AppComponent.dragulaService: DragulaService

this.dragulaService.drop("dnd")
      .subscribe(({ name, el, target, source, sibling }) => {
      //content
}

1.5.0(工作)

this.dragulaService.setOptions('dnd', {
  moves: (el, source, handle, sibling) => !el.classList.contains('nodrag')
});

2.1.1(工作)

this.dragulaService.drop("dnd")
    .subscribe(({ name, el, target, source, sibling }) => {
    //content
}

2.1.1(不工作)

this.dragulaService.createGroup("dnd", {
  moves: (el, source, handle, sibling) => !el.classList.contains('nodrag')
});

(Error):

Argument of type '{moves: (el: any, source: any, handle: any, sibling: any) => boolean; }' is not assignable to parameter of type 'DragulaOptions'. Object literal may only specify known properties, and 'moves' does not exist in type 'Dragula Options'. (parameter) handle: any

注意有 migration guide and changelog it does state how to replace the setOptions into create group。但它在我的情况下仍然不起作用。

有什么方法可以同时使用这两个功能吗?还是我漏掉了一些明显的东西?

你想:创建群组并同时使用拖放功能吗?

我们将createGroupdrop一起使用,但有一点不同——在订阅中添加了drop服务,但我认为这没有任何关系。所以我们的代码在这里:

export class xxxComponent implements OnInit {
    ...
    public dragula$: Subscription;
    ...

    constructor(public dragulaService: DragulaService) {
        this.dragulaService.createGroup('ITEMS', {
            direction: 'vertical',
            moves: (el, source, handle): boolean => handle.className.indexOf('item-keeper') > -1
        });
    }

    ngOnInit() {
        this.dragula$.add(this.dragulaService.drop('ITEMS')
            .subscribe(({name, el}) => {
                ....
            })
        );
    }

}

我认为根据有关事件的文档,我们的解决方案是正确的: https://github.com/valor-software/ng2-dragula#events

最后,我认为您的问题是 class name 的错误,因为其他所有内容在您的 Stackblitz 示例中都能完美运行。如果您取消注释:

    this.dragulaService.createGroup("dnd", {
      moves: (el, source, handle, sibling) => !el.classList.contains('nodrag')
    });

你的标签是不可拖动的,所以你得到你需要的。

这是对我有用的方法。

导入这些依赖项。

import { DragulaService } from 'ng2-dragula';
import { Subscription } from 'rxjs/Subscription';

.ts 文件 -


MANY_ITEMS = 'MANY_ITEMS';
subs = new Subscription();

  constructor(private dragula: DragulaService) { 

    this.subs.add(dragula.dropModel(this.MANY_ITEMS)
      .subscribe(({ el, target, source, sourceModel, targetModel, item }) => {

        console.log(el, target, source, sourceModel, targetModel, item);

      })
    );
  }

.html 文件 -


<ul [(dragulaModel)]='arrayList' [dragula]="MANY_ITEMS">
  <li>one</li>
  <li>two</li>
</ul>


您将能够拖动 'one' 和 'two' 列表项。