简体   繁体   English

无法通过对象文字定义应用属性值

[英]Property value does not get applied via object literal definition

I have the most bizzare problem. 我有最奇怪的问题。 I have a module that aggregates other imported modules and exports them in a single object. 我有一个模块,可聚合其他导入的模块并将其导出到单个对象中。 All objects from one of the imports (from Person module) do not get applied to their corresponding properties of the 'modules' object. 导入(来自“人”模块)之一中的所有对象都不会应用于“模块”对象的相应属性。 Objects from other imports do get applied properly. 其他导入对象确实可以正确应用。

import { Person, MiniPerson } from './Person';
import { Project, MiniProject } from './Project';

export const models = {
    Project: Project, // does get applied properly
    MiniProject: MiniProject, // does get applied properly
    Person: Person, // does not get applied
    MiniPerson: MiniPerson // does not get applied
};

function foo () {
    console.log(models); // returns {
        Project: someValue, MiniProject: someValue, 
        Person: undefined, MiniPerson: undefined }
    console.log(models.Person); // returns undefined
    console.log(Person); // returns a value (import is fine)
    models.Person = Person;
    console.log(models.Person); // returns a value
}

Remarks: 备注:

1) All imported objects are analogical and assigned to the aggregating model in the same way, I never had any problems with importing them in other modules (also the ones from Person module). 1)所有导入的对象都是类推的,并且以相同的方式分配给聚合模型,将它们导入其他模块(也包括来自Person模块的对象)从未遇到任何问题。

2) Objects form Person import seem to be imported properly - when I run the foo function they have a value, but the corresponding property of 'models' is undefined. 2)从Person导入的对象似乎已正确导入-当我运行foo函数时,它们具有值,但是'models'的相应属性未定义。 However, the property itself does exist (when logging the 'models' object (or Object.keys(models)), but its value is just undefined! 但是,属性本身确实存在(在记录“模型”对象(或Object.keys(models))时,但其值尚未定义!

3) If I assign the property again to the 'models' object in the foo function it does get assigned just fine and no longer returns undefined. 3)如果我再次将属性分配给foo函数中的'models'对象,它的确分配得很好,并且不再返回未定义。

4) I tried restarting computer, creating new file, but it didn't change anything. 4)我尝试重新启动计算机,创建新文件,但没有任何改变。

What can be the reason? 可能是什么原因? Where else to look? 还有什么地方看? I can use some 'init' function as workaround, but I'd like to avoid this. 我可以使用一些“ init”函数作为解决方法,但我想避免这种情况。

The most likely cause for this sort of behavior is a circular dependency between this module and the Person module. 此类行为的最可能原因是此模块与Person模块之间的循环依赖关系。 The general way of fixing this is to extract the shared dependency into a separate file. 解决此问题的一般方法是将共享依赖项提取到单独的文件中。

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

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