N
N
NiceScript2019-01-02 19:09:03
Vue.js
NiceScript, 2019-01-02 19:09:03

How to make auto import in Vue?

I continue to understand Vue.js,
There is a request to the server => the server pulls the necessary data and which components should be used (componentName: 'componentPath'):

used_components: 
{
   first: '@/components/first',
   second: '@/components/second'
},
......

Now I just load the components manually
.....
name: 'Page',
    components: {
      first: () => import('@/components/first'),
      second: () => import('@/components/second')
    },
    data () {
      return {
        server_array: '',
        server_components: ''
      }
    },....

How to import the necessary components based on the server response?
something like
name: 'Page',
    components:  server_components, // из даты берём (или как-то подругому)
    data () {
      return {
        server_array: '',
        server_components: '' //переопределим через methods
      }
    },
beforeMount () {
      this.getPage()
    },
methods: {
      async getPage () {
        const response = await Page.getPage()
        this.server_components = response.data.used_components
        this.setComponents(this.server_components)
      },
      setComponents (data) {
        for (var key in data) {
          () => import(data[key])
        }
      }
    }
....

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
levchak0910, 2019-01-03
@levchak0910

data: {
    componentName: ""
}
async getPage () {
    const response = await Page.getPage()
    this.componentName = ...
}

https://vuejs.org/v2/api/#component

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question