VueJS / Javascript - components and variables

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 is a better understanding:

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

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

var router = [ ...API_Request ]

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

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

Thank you!

    
asked by anonymous 02.10.2017 / 21:49

1 answer

2

I suggest creating an object so you can reference keys with these strings.

Something like this:

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')
    
02.10.2017 / 21:59