[英]React axios failed to compile
I'm new to React and got a Failed to compile error when trying to get api data with axios.我是 React 新手,尝试使用 axios 获取 api 数据时出现编译失败错误。
Axios.js: Axios.js:
import axios from 'axios'
const instance = axios.create({
baseURL: 'http://localhost:8001'
})
export default instance;
Cards.js: Cards.js:
import React, { useState, useEffect } from 'react'
import "./Cards.css"
import TinderCard from "react-tinder-card"
import axios from './Axios'
function Cards() {
const [people, setPeople] = useState([]);
useEffect(() => {
async function fetchData () {
const req = await axios.get("/tinder/cards")
setPeople(req.data);
}
fetchData()
}, [])
const swiped = (direction, nameToDelete) => {
console.log("removing: " + nameToDelete);
// setLastDirection(direction)
}
const outOfFrame = (name) => {
console.log(name + "left the screen!");
}
return (
<div className="Cards">
<div className="Cards__cardContainer">
{people.map((person) => (
<TinderCard
className="swipe"
key={person.name}
preventSwipe={["up", "down"]}
onSwipe={(dir) => swiped(dir, person.name)}
onCardLeftScreen={() => outOfFrame(person.name)}
>
<div
style={{ backgroundImage: `url(${person.url})`}}
className="card"
>
<h3>{person.name}</h3>
</div>
</TinderCard>
))}
</div>
</div>
)
}
export default Cards;
Error:错误:
Failed to compile.编译失败。
./src/Axios.js SyntaxError: C:\\Users\\suhai\\Documents\\evP\\Hinder\\h\\highlancer\\src\\Axios.js: Unexpected token (1:18) ./src/Axios.js SyntaxError: C:\\Users\\suhai\\Documents\\evP\\Hinder\\h\\highlancer\\src\\Axios.js: Unexpected token (1:18)
1 | 1 | import axios from axios从 axios 导入 axios
| | ^ ^
2 | 2 |
3 | 3 | const instance = axios.create({ const 实例 = axios.create({
4 | 4 | baseURL: 'http://localhost:8001' baseURL: 'http://localhost:8001'
看起来您的导入已关闭,试一试。
import axios from 'axios';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.