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

时间:2018-12-28 19:50:21

标签: api import 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。 我正在浏览器中运行它,但未使用任何捆绑程序,因此不知道为什么会发生这种情况?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

没有任何编译器或捆绑器,您将需要使用以下代码在HTML文件中添加axios

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

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