Axios和RequireJS之间的集成失败

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

我在RequireJS和Axios之间集成时出错:

<HEAD>
<script src="3party/require.js"></script> 

<SCRIPT>
//work!
 requirejs(['/bower_components/jquery/dist/jquery'],()=>{
        console.debug($);
     })

//error  
 requirejs(['/bower_components/axios/dist/axios'],()=>{
        axios.get('https://httpbin.org/get').then(function(response){ 
              console.log(response.status); // ex.: 200
            });  
     });
</SCRIPT>
</HEAD

URL是测试服务,发生以下错误。

require.js:5 Uncaught Error: Script error for "/bower_components/axios/dist/axios"
https://requirejs.org/docs/errors.html#scripterror
    at makeError (require.js:5)

任何URL,甚至下一行,都会发生错误:

console.debug(axios);

我正在使用以下版本:

"axios": "^0.19.2",
axios requirejs
1个回答
0
投票

我已经检查了AXIOS代码,它确实支持AMD。因此,您需要将其用作常规AMD:

<HEAD>
<script src="3party/require.js"></script> 

<SCRIPT>
 requirejs(['/bower_components/jquery/dist/jquery'],()=>{
        console.debug($);
     })

 requirejs(['/bower_components/axios/dist/axios'],(axios)=>{ // axios is given as a argument to a your callback
        axios.get('https://httpbin.org/get').then(function(response){ 
              console.log(response.status); // ex.: 200
            });  
     });
</SCRIPT>
</HEAD>

当您需要时,AXIOS将不会作为全局模块提供,而是作为本地模块提供:)

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