Dynamically Loading APIs
What type of issue are you creating?
- [ ] Bug
- [ ] Enhancement
- [x] Question
What version of this module are you using?
- [ ] 2.0.10 (Stable)
- [x] 2.1.0-rc.n (2.1 Release Candidate n)
- [ ] Other
Write other if any:
Please add a description for your issue:
I want to create an App Builder where the User can choose which Service to use dynamically.
What I have done so far is to import the sdk like this:
import * as sdkloopback from '../../shared/sdk/index';
and then in my constructor:
constructor(
public test: sdkloopback.EmployeeApi
) {
this.test.find()
// This is working perfectly
}
loadDynamically() {
const test2 = sdkloopback['EmployeeApi'];
test2.find()
// this is not working
}
Eventually I want EmployeeApi to be a Variable that depends on what the person is clicking,
Hi @dmastag thanks for reaching out, this sounds like fun project.
Hey so, there is a difference beteween your 2 methods... When you inject it Angular passes an instance of sdkloopback, while in contrast by loading dynamically you are not making an instance first.
for me it looks like this might help:
loadDynamically() {
const test2 = new sdkloopback['EmployeeApi']();
test2.find()
// what are the results?
}
The problem is that I think you are loosing the singletons benefit and you would create multiple instance of the same class, unless you handle that of course
Hi @jonathan-casarrubias I tried that and got this error ☹️
ERROR TypeError: Cannot read property 'get' of undefined
at PoliklinikApi.BaseLoopBackApi (base.service.ts:44)
at new EmpployeeApi (Employee.ts:31)
at CrudComponent.webpackJsonp.../../../../../src/app/crud/crud.component.ts.CrudComponent.loadData (crud.component.ts:116)
at SafeSubscriber._next (crud.component.ts:85)
at SafeSubscriber.webpackJsonp.../../../../rxjs/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:238)
at SafeSubscriber.webpackJsonp.../../../../rxjs/Subscriber.js.SafeSubscriber.next (Subscriber.js:185)
at Subscriber.webpackJsonp.../../../../rxjs/Subscriber.js.Subscriber._next (Subscriber.js:125)
at Subscriber.webpackJsonp.../../../../rxjs/Subscriber.js.Subscriber.next (Subscriber.js:89)
at MapSubscriber.webpackJsonp.../../../../rxjs/operator/map.js.MapSubscriber._next (map.js:83)
at MapSubscriber.webpackJsonp.../../../../rxjs/Subscriber.js.Subscriber.next (Subscriber.js:89)
I also tried using class.prototype to add it, but it didn't succeed.