意外的标识符"axios"。导入调用只需要一个参数



这是我的js代码:

import axios from 'axios';
const quotes = document.querySelector('.quotes');
const getResults = async () => {
    try {
        const res = await axios('http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1');
        quotes.innerHTML = res.data[0].content;
        console.log(res.data[0].content);
    } catch(err) {
        console.log(err);
    }
}
getResults();

它会产生以下错误:Unexpected identifier 'axios'. import call expects exactly one argument 。我在浏览器中运行它,我没有使用任何捆绑器,知道为什么会发生这种情况吗?

尝试对不同的 http 动词使用这些方法,例如:

const res = await axios.get('http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1');

在没有任何转译器或捆绑器的情况下,您需要使用以下代码在 html 文件中包含axios

<script type="module" src="https://unpkg.com/axios/dist/axios.min.js"></script>

并将其加载到包含要执行的脚本的文件之前。

最新更新