Wuxt.js(Nuxt.js)从axios响应中获取数据

时间:2019-10-09 09:40:58

标签: axios nuxt.js

我想使用Wuxt框架(Nuxt + Wordpress)从Wordpress json响应中提取菜单项,但无法在提取外部访问apiVersion: template.openshift.io/v1 kind: Template metadata: name: 'myapp' description: "myapp(myapp-image) deployment template" annotations: iconClass: "fa fa-certificate" description: "myapp" tags: "myapp" objects: - apiVersion: v1 kind: DeploymentConfig metadata: creationTimestamp: null generation: 1 labels: app: '${APPLICATION_NAME}' built-from: java-deploy-config-template name: '${APPLICATION_NAME}' spec: replicas: 1 selector: deploymentConfig: '${APPLICATION_NAME}' strategy: resources: {} rollingParams: intervalSeconds: 1 maxSurge: 25% maxUnavailable: 25% timeoutSeconds: 600 updatePeriodSeconds: 1 type: Rolling template: metadata: creationTimestamp: null labels: deploymentConfig: '${APPLICATION_NAME}' name: '${APPLICATION_NAME}' spec: containers: - env: - name: THRESHOLD value: '${THRESHOLD}' parameters: - description: >- Threshold to trigger the script displayName: Threshold value name: THRESHOLD required: true value: < 20 ~ 30 > 对象(错误消息是未定义数据) 这是我的代码

data

如何访问<script> import axios from 'axios' import Logo from '~/components/Logo' export default { components: { Logo }, async fetch ({ params, error }) { try { let { data } = await axios.get('http://localhost:3080/wp-json/wuxt/v1/menu') return data } catch (e) { error({ message: 'Not found', statusCode: 404 }) } } } </script> 对象以插入模板?

2 个答案:

答案 0 :(得分:1)

如果使用访存,则所有数据都应提交到存储中并从中访问。如果要返回数据,请使用asyncData方法。

答案 1 :(得分:0)

我不得不稍微修改一下代码,它返回一个带有变量的数据函数,所以看起来像这样。

export default {
  components: {
    Logo
  },
  data() {
    return { menus: [] }
  },
  mounted() {
    fetch('http://localhost:3080/wp-json/wuxt/v1/menu')
    .then(response => {
      response.json().then(menus => {
        this.menus = menus;
      })
    })
  }

}