简体   繁体   English

如何使Dragula成为Angular 2中的全球提供者以在组件之间共享

[英]How to make Dragula a global provider in Angular 2 to share across components

I'm trying to make it so that I can drag and drop nicely between components, but having Dragula set up the recommended way means that each component has its own copy of Dragula. 我正在尝试使其能够在组件之间很好地拖放,但是将Dragula设置为推荐的方式意味着每个组件都有其自己的Dragula副本。 So I can't drag and drop between the components. 所以我不能在组件之间拖放。

I've tried to make Dragula be a global provider using the UpgradeAdaptor, but possibly that's not the right way to go about it. 我试图使用UpgradeAdaptor使Dragula成为全球提供商,但是这可能不是正确的解决方法。 I'm quite new to Angular 我对Angular很陌生

Here is the component that I'm hoping to reuse: 这是我希望重用的组件:

import {Component, Input, Inject}   from "@angular/core";
import {Dragula, DragulaService}   from "ng2-dragula/ng2-dragula";

@Component({
  selector: "drag-container",
  template: require("../templates/draggable/drag-container"),
  directives: [Dragula],
  viewProviders: [],
  providers: []
})

export class DragContainer<T> {
  @Input() items: Array<T>;
  @Input() edit: boolean;

  constructor(private dragulaService: DragulaService) { }
}

And where I'm trying to make Dragula be global: 在我试图使Dragula成为全球性的地方:

import {UpgradeAdapter}                             from "@angular/upgrade";
import {DragulaService}                             from "ng2-dragula/ng2-dragula";

export const adapter = new UpgradeAdapter();

window["foo"].coursesAdapter = adapter;

// Angular 2 services
adapter.addProvider(DragulaService); 

// Angular 1 services we want to use in Angular 2 components
adapter.upgradeNg1Provider("$routeParams");
adapter.upgradeNg1Provider("translate");
adapter.upgradeNg1Provider("showError");
adapter.upgradeNg1Provider("$location");

And right now, I can still drag and reorder within the component, but I can't move it across the components. 现在,我仍然可以在组件中拖动和重新排序,但是不能在组件之间移动它。 And they have the same bag name. 而且他们有相同的包名。

Keep in app component: 保留在应用程序组件中:

directives: [Dragula],
encapsulation: ViewEncapsulation.None,
viewProviders: [DragulaService]

In subcomponents, just: 在子组件中,只需:

    directives: [Dragula]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM