Nuxt.js:未定义ReferenceError baseURL

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

我有一个Nuxt.js应用程序。我想读取位于static /文件夹中的本地JSON文件。

我在我的nuxt.config.js文件中:

module.exports = {                                                                                                                                                       
    modules: [                                                                                                                                                           
        ['@nuxtjs/axios', {                                                                                                                                              
            baseURL: 'http://localhost:3000',                                                                                                                            
            browserBaseURL: 'http://localhost:3000'                                                                                                                      

        }]                                                                                                                                                               
    ],                                                                                                                                                                   

    axios: {                                                                                                                                                             
        // proxyHeaders: false                                                                                                                                           
        baseURL: "http://localhost:3000/"                                                                                                                                
    },        

在我的pages / index.vue中:

<template>                                                                                                                                                          
<section class="container">                                                                                                                                  
  <p>{{ data }}</p>                                                                                                                                                
</section>                                                                                                                                                               
</template>                                                                                                                                                              

<script>                                                                                                                      
import axios from 'axios'                                                                                                                                                

export default {                                                                                                                                                         
    components: {                                                                                                                                                        
        Make                                                                                                                                                             
    },                                                                                                                                                                   
     async asyncData({ app }) {                                                                                                                                        
      const data = await app.$axios.$get(baseURL + 'static/db.json')                                                                                                         
      return { data }                                                                                                                                                    
     }                                                                                                                                                                 

}       
</script>

我收到此错误:

 ReferenceError
 baseURL is not defined

截图:

enter image description here

当我对URL进行硬编码时:

async asyncData({ app }) {                                    
        const data = await app.$axios.$get('http://localhost:3000/static/db.json')                                                                                     
        return { data }                                                                                                                                                    
    }    

我收到此错误消息:

NuxtServerError
Request failed with status code 404

有帮助吗?

参考:https://axios.nuxtjs.org/

javascript asynchronous axios nuxt.js nuxt
1个回答
0
投票

如果将baseURL设置为模块选项,则无需将baseURL添加到axios调用中。它将自动添加。

这不是它的工作原理。如果你在配置中定义某些内容,它就不会在你的代码中神奇地出现。应该定义或导入您引用的任何内容。

© www.soinside.com 2019 - 2024. All rights reserved.