简体   繁体   English

Nuxt.js:未定义 ReferenceError baseURL

[英]Nuxt.js: ReferenceError baseURL is not defined

I have a Nuxt.js app.我有一个 Nuxt.js 应用程序。 I want to read a local JSON file located in the static/ folder.我想读取位于static/文件夹中的本地 JSON 文件。

I have in my nuxt.config.js file:我的nuxt.config.js文件中有:

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

        }]                                                                                                                                                               
    ],                                                                                                                                                                   

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

In my pages/index.vue :在我的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>

I am getting this error:我收到此错误:

 ReferenceError baseURL is not defined

Screenshot:截屏:

在此处输入图片说明

When I hard code the URL:当我对 URL 进行硬编码时:

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

I get this error message instead:我收到此错误消息:

 NuxtServerError Request failed with status code 404

Any help?有什么帮助吗?

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

You don't need to add baseURL into axios call if you set it into module options.如果将 baseURL 设置为模块选项,则不需要将 baseURL 添加到 axios 调用中。 It will be prepended automatically.它将被自动添加。

And thats not how it work.这不是它的工作原理。 If u define something in config it dont appear magically in your code.如果你在 config 中定义了一些东西,它不会神奇地出现在你的代码中。 Anything you reference should be defined or imported.你引用的任何东西都应该被定义或导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM