Axios出现错误:无法读取未定义的属性'$ get'

问题描述 投票:0回答:2

我尝试利用Axios来访问API,但收到错误:

控制台中的错误是NUXT SSR:

TypeError: Cannot read property '$get' of undefined
at asyncData (pages/index.js:98:35)
at promisify (server.js:1898:15)
at Promise.all.Components.map.Component (server.js:1573:82)
at Array.map (<anonymous>)
at module.exports../.nuxt/server.js.__webpack_exports__.default (server.js:1569:51)

这是我尝试这样做的方式:

export default {

async asyncData({ $axios }) {
  try {
    let response = await $axios.$get("http://localhost:3000/api/products");
    console.log(response);
    return {
      products: response.products
    };
  }catch (err) {

    console.log(err);

  }
}

}

API本身有效。如果我从浏览器中调用它-它会给我产品列表。我究竟做错了什么?

完整填充:

<template>

<div class="container-fluid browsing-history">
  <div class="row">


    <div class="col-sm-8 col-8">

      <h1 class="a-size-large a-spacing-none a-text-normal">All products</h1>
      <div class="a-spacing-large"></div>

      <a href="#" class="a-button-buy-again">Add new product</a>
      <a href="#" class="a-button-history margin-right-10">Add new category</a>
      <a href="#" class="a-button-buy-again margin-right-10">Add new owner</a>
    </div>
  </div>
</div>
<div class="a-spacing-large"></div>

<div class="container-fluid browsing-history">
  <div class="row">
    <div class="col-xl-2 col-lg-2 col-md-3 col-sm-6 col-6 br bb">

      <div class="history-box">
        <!--          product page-->
        <a href="#" class="a-link-normal">
          <img src="" class="img-fluid" alt="">
        </a>

        <div class="a-spacing-top-base asin-title">
        <span class="a-text-normal">
          <div class="p13n-sc-truncated">Title </div>
        </span>
        </div>

        <div class="a-row">
          <a href="#">
            <i class="fas fa-star"></i>
            <i class="fas fa-star"></i>
            <i class="fas fa-star"></i>
            <i class="fas fa-star"></i>
            <i class="fas fa-star"></i>
          </a>
          <span class="a-letter-space"></span>
          <span class="a-color-tertiary a-size-small asin-reviews">(1732)</span>
        </div>
      </div>
      <!--price-->
      <div class="a-row">
        <span class="p13-sc-price">$25</span>
      </div>
      <!--byttons-->
      <div class="a-row">
        <a href="#" class="a-button-history margin-right-10">Update</a>
        <a href="#" class="a-button-history margin-right-10">Delete</a>
      </div>
    </div>
  </div>
</div>

导出默认值{异步asyncData({$ axios}){尝试{let response =等待$ axios。$ get(“ http:// localhost:3000 / api / products”);console.log(response);返回{产品:产品};} catch(err){console.log(err);}}};

我确实确保已安装。我还包括

module.exports = {
  modules: [
    '@nuxtjs/axios',
  ],

  axios: {
    // proxyHeaders: false
  }
}

就像他们在安装手册中说的那样。还尝试删除$,然后再获取...在控制台中仍然出现相同的错误:(我不知道还有什么错误。.

vue.js axios nuxt.js
2个回答
0
投票

似乎您不包括axios模块,请在此处查看安装过程https://axios.nuxtjs.org/setup.html#install


0
投票

这里是axios in Nuxt的步骤

  1. 确保已安装axios npm install @nuxtjs/axios
  2. 在nuxt.config.js中添加axios
...
  axios: {
    // proxyHeaders: false
  }
...
  1. 您不需要$中的$axios.$get。应该是$axios.get()
© www.soinside.com 2019 - 2024. All rights reserved.