簡體   English   中英

Nuxt:如何在 fetch() 方法中訪問 axios?

[英]Nuxt: how do I access axios within the fetch() method?

我正在使用 nuxt 並且正在尋找一種從 nuxt fetch()方法中訪問axios對象的方法。 不幸的是,我似乎無法訪問它。 這是我試過的...

    async fetch({store, params}) {
        // const result = await axios.$get('/api/v2/inventory/3906?apiKey=xxx');
        // const result = await this.axios.$get('/api/v2/inventory/3906?apiKey=xxx');
        // const result = await $axios.$get('/api/v2/inventory/3906?apiKey=xxx');
        store.commit('property/setProperty', result);
    }

this (顯然)不可用,並且$axiosaxios未定義。

我確實在我的nuxt.config.js定義了@nuxtjs/axios nuxt.config.js

modules: [
    'nuxt-buefy',
    '@nuxtjs/style-resources',
    '@nuxtjs/device',
    '@nuxtjs/axios',
    '@nuxtjs/proxy'
],

@balexandre 實際上應該為此獲得贊譽。 謝謝您的幫助。

這是我問的一個非常愚蠢的問題,但我會在這里發布答案,以防其他人有同樣的經歷。

import axios from 'axios' // don't forget me!

... later...

fetch() {
  async fetch({store, params}) {
    const result = await axios.get('/api/v2/inventory/3906?apiKey=xxx');
    store.commit('property/setProperty', result);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM