V
V
Viktor2018-11-07 12:48:41
Vue.js
Viktor, 2018-11-07 12:48:41

How to avoid switch when implementing a component that works with different APIs/entities in VueJS?

There is a component that is a form for uploading scans. The scan is attached to a document, and documents are of different types and have different APIs/endpoints for them. Accordingly, visually the component looks the same, but it should load scans at different URLs.
One solution is to pass type to the component, and write something like this in the component itself:

switch (this.type) {
    case 'oneDoc': 
        oneDocApi.uploadScan(...); return
    case 'anotherDoc':
        anotherDocApi.uploadScan(...); return
}

Is there a better solution? I just don't like using the switch.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-11-07
@victor-ponamariov

// где-то вне метода загрузки сканов
const docApi = {
  oneDoc: oneDocApi,
  anotherDoc: anotherDocApi,
  ...
};

docApi[this.type].uploadScan();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question