【问题标题】:Axios not working with <Route path="url/:id">Axios 不适用于 <Route path="url/:id">
【发布时间】:2021-03-14 16:00:17
【问题描述】:

Produit 组件的路径是带有 id 时,我无法让 Axios 工作:

<Route exact path="/produit/:id" component={Product} />

但是没有 :id Axios 工作正常:

<Route exact path="/produit" component={Product} />

Axios 的 Produit 组件:

//for the moment I do not use id
const { id } = useParams();
//im doing a Get 
const getAvisData = async () => {
    try {
        const result = await Axios.get(`api/avis?produit=1`).then(res => {
            console.log(res);
            setComments(res.data['hydra:member']);
            setCountAvis(res.data['hydra:totalItems']);
        }).catch(error => {
            console.error(error);
        });
    } catch (error) {
        console.error(error);
    }
}

useEffect(() => {
    getAvisData();
}, []);

【问题讨论】:

    标签: reactjs routes axios router


    【解决方案1】:

    我发现当我使用这条路径时

    path="/produit/:id"

    我需要将整个 url 设置为 api :

    Axios.get(`http://127.0.0.1:8000/api/avis?produit=1`)
    

    【讨论】:

      猜你喜欢
      • 2019-07-07
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      • 2019-03-27
      • 2019-09-22
      • 1970-01-01
      • 2018-08-16
      相关资源
      最近更新 更多