【问题标题】:Getting error second time getting to page with ng2-dragula第二次使用 ng2-dragula 进入页面时出错
【发布时间】:2017-05-02 04:34:52
【问题描述】:

我试图阻止用户移动dragulaService 的第一个元素,代码第一次运行没有任何错误,但是当我离开这个页面然后再次打开它时,我得到了错误。

导致错误的代码:

constructor(public service:SmartTablesService, private dragulaService:DragulaService) {
    dragulaService.setOptions('nested-bag', {
      revertOnSpill: true,
      moves: function (el:any, container:any, handle:any):any {
        if (handle.className === 'sorting-table-title') {
          return false;
        } else {
          return true;
        }

      }
    });

错误是:

error_handler.js:48 例外:未捕获(在承诺中):错误:./SortTableComponent 类 SortTableComponent_Host 中的错误 - 内联模板:0:0 导致:包名为:“nested-bag”已存在。 错误:包名为:“nested-bag”已经存在。 在 DragulaService.add (http://platform.local:8080/3.chunk.js:1070:19) 在 DragulaService.setOptions (http://platform.local:8080/3.chunk.js:1099:24) 在新的 SortTableComponent (http://platform.local:8080/3.chunk.js:1311:24)

【问题讨论】:

    标签: angular drag-and-drop ng2-dragula


    【解决方案1】:

    您需要在组件的onDestroy 生命周期中手动销毁nested-bag,因为它不会自动完成:

    ngOnDestroy() {
        this.dragulaService.destroy('nested-bag');
    }
    

    【讨论】:

      【解决方案2】:

      最好为每个使用拖放的Angular组件提供唯一的DragulaService,而不是共享公共服务:

      @Component({
         ...,
         providers: [DragulaService],
         ...
      })
      export class MyComponent {
      ...
      }
      

      Source

      【讨论】:

        猜你喜欢
        • 2014-02-06
        • 2018-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-22
        • 1970-01-01
        相关资源
        最近更新 更多