action.js
import Axios from 'axios';
import { PRODUCT_LIST_FAIL, PRODUCT_LIST_REQUEST, PRODUCT_LIST_SUCCESS } from '../constraints/productConstraints';
const listProducts = () => async (dispatch) => {
try {
dispatch({ type: PRODUCT_LIST_REQUEST });
const { data } = await Axios.get('http://localhost:3001/product');
dispatch({ type: PRODUCT_LIST_SUCCESS, payload: data });
}
catch(error) {
dispatch({ type: PRODUCT_LIST_FAIL, payload: error.message });
}
}
export { listProducts }
home.js
const Home = () => {
const productList = useSelector(state => state.productList);
const { products, loading, error } = productList;
const dispatch = useDispatch();
useEffect(() => {
dispatch(listProducts());
return () => {
}
},[]);
}
当我使用axios.get('/product'(时,它有效,但当我使用axios.get('http://localhost:3001/product'(显示错误GEThttp://localhost:3001/productnet::ERR_CONNECTION_REFUSED不知道为什么我得到这个错误API没有命中
如果您还没有这样做,请在React应用程序中将"proxy": "http://localhost:3001",
添加到package.json
中。