如何在Aurelia对话框中从DI容器中注入对象

时间:2017-05-02 12:54:20

标签: javascript ecmascript-6 aurelia aurelia-dialog aurelia-di

使用Aurelia,您可以将东西放入容器中然后注入。容器状态不在对话框中共享。

有没有办法获取我在容器中注入的数据,并在对话框中使用它?

代码示例

home.js

fetchString(){
  this.data = JSON.parse(stringform);
  this.container.registerInstance('tpscform', this.data);
}

自定义-element.js

import {DialogService} from 'aurelia-dialog';
import {LookupFieldsDialog} from './dialog/lookup-fields-dialog';

@inject('tpscform', DialogService)
export class LookupFieldsQuestion {

  constructor(form, dialog){
    console.log(form); // returns the object from the container - works
    //...
  }

  submit() {
    this.dialogService.open({
      viewModel: LookupFieldsDialog,
      model: this.question,
      lock: false
    });
  }

}

查找字段-dialog.js

import {inject} from 'aurelia-framework';
import {DialogController} from 'aurelia-dialog';

@inject(DialogController, 'tpscform')
export class LookupFieldsDialog {

  constructor(controller, form) {
    this.controller = controller;
    console.log(form); // Returns the string 'tpscform' - doesn't work
  }

  activate(question) {
    this.question = question;
  }

}

1 个答案:

答案 0 :(得分:1)

正如@FabioLuz所提到的,您可以将容器传递到对话框中:

replace