第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在 React 中將變量傳遞給 axios 請(qǐng)求

在 React 中將變量傳遞給 axios 請(qǐng)求

茅侃侃 2022-10-21 15:25:16
我有這個(gè) axiosget方法可以從端點(diǎn)檢索用戶擁有的所有播放列表:getUserPlaylists() {        axios            .get("/v1/users/"+ JSON.parse(localStorage.getItem("user")).id +"/playlists", {headers: { Authorization: localStorage.getItem("apiKey") }})            .then((response) => {                var playlists = [];                for (var i = 0; i < response.data.items.length; i++) {                    playlists[i] = response.data.items[i];                }                console.log(response);                this.setState({                    playlists: playlists,                });            })            .catch((error) => console.log("Get Error"));    }效果很好,我還編寫了一種方法來使用和 將post當(dāng)前視頻添加到播放列表videoIDplaylistIDaddToPlaylist(playlistID){        const uri = "/v1/users/"+ JSON.parse(localStorage.getItem("user")).id +"/playlists/"+ playlistID +"/videos"      axios          .post(uri, {id: window.location.href.substring(28) }, {headers: { Authorization: localStorage.getItem("apiKey") }})          .then((response) => {              console.log(response);          })          .catch((error) => console.log("Get Error"));    }我嘗試在post方法中調(diào)用render方法如下:{this.state.playlists.map((playlist) => (<ul key={playlist.id}><Button type="button" className="btn btn-primary" onClick={this.addToPlaylist.bind(playlist.id)}>Add video to Playlist <b><i>{playlist.name}</i></b></Button></ul>))}一切正常,除了當(dāng)我單擊按鈕調(diào)用該post方法時(shí),我收到以下錯(cuò)誤POST http://localhost:3000/v1/users/867/playlists/[object%20Object]/videos 404 (Not Found)我不明白為什么該post方法無法正確獲取playlistID
查看完整描述

1 回答

?
不負(fù)相思意

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊

來自 MDN 文檔:

bind()方法創(chuàng)建一個(gè)新函數(shù),在調(diào)用該函數(shù)時(shí),將其 this 關(guān)鍵字設(shè)置為提供的值,并在調(diào)用新函數(shù)時(shí)提供的任何參數(shù)之前具有給定的參數(shù)序列。

bind 的第一個(gè)參數(shù)是上下文(this 的值),在您當(dāng)前的場(chǎng)景中,您可以將其設(shè)置為 null。

onClick={this.addToPlaylist.bind(null, playlist.id)}

或者您可以使用箭頭功能

onClick={() => this.addToPlaylist(playlist.id)}


查看完整回答
反對(duì) 回復(fù) 2022-10-21
  • 1 回答
  • 0 關(guān)注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)