How to properly create dictionaries for each recor

2019-08-18 07:43发布

I am trying to create a dictionary for each record returned in an API call.

my broken code:

import lazyLoading from './lazyLoading'

// get records from api call

created() {
  axios.get('http://localhost:8080/api/tools/')
  .then(response => {
       this.json_data = response.data
       console.log(this.json_error)
  })
  .catch(error => {
    console.log(error);
  })

export default {
  name: 'test',
  meta: {
    icon: 'fa-android',
    expanded: false
  },

const children = [];
json_data.forEach(item => {
    const dict = {
       name: item.name,
       path: item.path,
       meta:  {
           label: item.label,
           link: item.link,
       },
       component: lazyLoading('testitem/basic'),
    }
    children.push(dict);
});

}

desired result:

  export default {
    name: 'test',
    meta: {
      icon: 'fa-android',
      expanded: false
      },

  children: [
    {
      name: 'test',
      path: 'test',
      meta: {
        label: 'test',
        link: 'test'
      },
      component: lazyLoading('test/basic')
    },

    {
      name: 'test',
      path: 'test',
      meta: {
        label: 'test',
        link: 'test'
      },
      component: lazyLoading('test/Basic')
    },

    {
      name: 'test',
      path: 'test',
      meta: {
        label: 'test',
        link: 'test'
      },
      component: lazyLoading('test/Basic')
    }
  ]

(obviously 'test' would be replaced what is returned in the api). The main problem is I don't know how to dynamically create the dictionarys. I also have no idea how to view/troubleshoot my axios request. I assumed console.log would spit out the object into the chrome dev tools under console section but I don't see the object there. I'm completely new to javascript so maybe I'm not looking in the correct spot.

Also I'm getting this error:

Module build failed: SyntaxError: 'import' and 'export' may only appear at the top level

So where do I put my api request if I cannot put it at the top?

0条回答
登录 后发表回答