Vue doesn't have a built-in DI container for handling this scenario. Ideally, there would be a mapping system connecting classes or factory functions to specific string identifiers:
const servicesMap = {
Foo,
Bar
};
...
if (!Object.keys(servicesMap).includes(name))
throw ...
const Service = servicesMap[name];
const service = new Service();
If all services are contained within a single folder and need to be dynamically imported, this can be achieved using dynamic imports:
const Service = require(`.../services/${name}`).default;
const service = new Service();
However, it's important to note that this approach relies on project configuration and may impact treeshaking optimizations for services
.