gpt4 book ai didi

javascript - 如何从 Aurelia-dialog 中的 DI 容器注入(inject)对象

转载 作者:行者123 更新时间:2023-11-29 23:46:41 24 4
gpt4 key购买 nike

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

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


代码示例

home.js

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

custom-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
});
}

}

lookup-fields-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;
}

}

最佳答案

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

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

@inject(DialogService, Container)
export class LookupFieldsQuestion {

constructor(dialogService, container) {
this.container = container;
this.dialogService = dialogService;
}

submit() {
this.dialogService.open({
viewModel: LookupFieldsDialog,
model: this.question,

// Share current container here
childContainer: this.container.createChild(),

lock: false
});
}

关于javascript - 如何从 Aurelia-dialog 中的 DI 容器注入(inject)对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43738446/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com