本文介绍了接受参数的依赖注入构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Aurelia应用,该应用对每种类型的数据对象都使用模型".

I'm building an Aurelia app that uses "Models" for every type of data object.

我所有的模型看起来都像这样:

All my Models look something like this:

export class Item {
    id = null;
    name = '';
    description = '';

    constructor (data) {
        Object.assign(this, data);
    }
}

后来我创建了这样的对象:

And I later create objects like this:

export class SomeViewModel {
    activate () {
        this.myItem = new Item({
            name: 'My Item!',
            description: 'This is my item. It will be initialized with these properties.'
        });
    }
}

我从阅读的文章中得到了Object.assign()位,它的工作原理非常好.它使我可以使用服务器中的数据来创建新项目,或者如果我想要一个空项目,那么我根本不会传入任何内容.

I got the Object.assign() bit from an article I read and it works really well. It allows me to create new items using data from the server, or if I want an empty Item I simply don't pass in anything.

现在我已经到达需要我的模型访问另一个类的地步,所以我正在使用像这样的Aurelia的依赖注入:

Now I've reached a point where I need my model to have access to another class so I'm using Aurelia's Dependency Injection like this:

import {inject} from 'aurelia-framework';
import {Router} from 'aurelia-router';

@inject(Router)
export class Item {
    id = null;
    name = '';
    description = '';

    constructor (router, data) {
        this.router = router;
        Object.assign(this, data);
    }

    get permalink () {
        return window.location.protocol + '//' + window.location.host + this.router.generate('item', {itemId: this.id});
    }
}

现在我的问题是这个;如何创建new Item()而不自己传入Router?我想将参数顺序切换为constructor()可以解决问题,但这似乎不适用于Aurelia?

Now my problem is this; how do I create a new Item() without passing in the Router myself? I guess switching the order of argument to constructor() would do the trick but that doesn't seem to work with Aurelia?

而且我不想每次创建新项目时都必须这样做:

And I don't want to have to do this every time I create a new Item:

import {inject} from 'aurelia-framework';
import {Router} from 'aurelia-router';

@inject(Router)
export class SomeViewModel {
    constructor (router) {
        this.router = router;
    }

    activate () {
        this.myItem = new Item(this.router, {
            name: 'My Item!',
            description: 'This is my item. It will be initialized with these properties.'
        });
    }
}

肯定有更好的方法可以解决这个问题吗?

Surely there must be a better way to solve this?

推荐答案

使用Factory解析器.例如: https://gist.run?id=46642ac54893186067e7cd890d6722a3 **

Use the Factory resolver. Here's an example: https://gist.run?id=46642ac54893186067e7cd890d6722a3**

import {inject, Factory} from 'aurelia-dependency-injection';
import {MyModel} from './my-model';

@inject(Factory.of(MyModel))
export class App {
  message = 'Hello World!';

  constructor(createModel) {
    let model = createModel('my data');
  }
}

my-model.js

import {inject} from 'aurelia-dependency-injection';
import {EventAggregator} from 'aurelia-event-aggregator';

@inject(EventAggregator)
export class MyModel {
  constructor(eventAggregator, data) {
    console.log(eventAggregator, data);
  }
}

这篇关于接受参数的依赖注入构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 11:16
查看更多