Vuejs/Javascript - components and variables

Asked

Viewed 68 times

1

Hello! I have a restAPI that returns me a array of names, I wanted to know how I do to get them as componentes. Below you can understand better:

import produtos from './produtos'
import artigos from './artigos'

var API_Request = ["produtos", "artigos"]

var router = [ ...API_Request ]

const app = new Vue({
    router
}).$mount('#container')

The API_Request comes as string, how do i convert it to components that were imported above?

Thank you!

  • 1

    These Api_request strings are always some of the modules you know to have right? ie you can have import of x modules that the API will always refer to them right?

  • These Api_request strings always come modules that I’m sure I have

1 answer

2


I suggest you create an object so you can refer keys with these strings.

Something like that:

import produtos from './produtos'
import artigos from './artigos'

const componentes = {
    produtos: produtos,
    artigos: artigos
}

var API_Request = ["produtos", "artigos"]

var router = API_Request.map(key => componentes[key])

const app = new Vue({
    router
}).$mount('#container')
  • 1

    Thank you so much!! It worked perfectly :) ! I’m needed to add the arrays creating with : var mergeArrays = [].concat.apply([], router);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.