Can you get around multiple imports

I have a provider with constants that I use throughout my app; it is imported in app.module and the pages.ts I use it from. I am unsure of the relationship between what is happening in /app vs /pages, and am a little uncomfortable with the same “import” (with pathing differences) on multiple pages, and am wondering if there is a “best practices” way to do this once.

How imports are handled depends on the Ionic Framework version you are using, so that’s something to research.

Anyway, I don’t know about best practices. I think we’re making it up as we go along, and the distillate of good ideas is best-we-know-how practices. But I’ll tell you what I do.

I have a models folder, and inside it I have a file for each “object type.” The “objects” are defined as interfaces, and the new object operator is instead a function newObject that returns a deep copy of a new object. (This is all to increase immutability of objects, to avoid the hard-to-debug errors that arise because Javascript aggressively creates pointers and shallow copies.) Constants associated with those objects appear there, as export const.

I try to keep all exported constants in the appropriate model folder, because there are seldom constants that belong to the provider. Constants almost always describe some entity, so I model that entity in the models folder, and the providers cause those entities to interact. Example: how many characters long is a preview? That goes in the preview model folder. Then the makePreviewProvider imports the number of characters the preview is supposed to be and makes the preview. So if I ever want to change anything about the preview object, it’s all in the same file location.

1 Like