2017-11-29 3 views
1

を要求エラーを処理?おかげさまで 私は、Webアプリケーションでaxiosを使用してGETリクエストをした、独自のPARAMATERSで

+3

トンの定義彼は関数呼び出しの外でparams? – evolutionxbox

+0

私はリクエストを複数回使用します。リクエストが完了する前に上書きすることができます。非同期にする必要があります。 –

+0

hmm、params上の 'closure'はここで仕事をしませんか? –

答えて

2

あなたは文脈でそれらを保つためのパラメータの上に(閉鎖)閉じることもできます。これは、テストされていなかった、

function myWrapper() { 
    const params = { 
    a: 'a', 
    b: 'b' 
    } 
    axios({ 
     method: 'get', 
     url: "example.com", 
     params: params 

    }) 
    .then((response) => { 
     // you have access to params here 
     console.log(params.a); 
     //do stuffs 
    }) 
    .catch((error) => { 
     // you have access to params here 
     console.log(params.a); 
     // need params._id here ?!! 
    }) 
} 

が、ジョブここで

を行う必要があります実行している例です。 :

function myWrapper() { 
 
    const params = { 
 
    a: 'a', 
 
    b: 'b' 
 
    } 
 
    axios({ 
 
     method: 'get', 
 
     url: "example.com", 
 
     params: params 
 

 
    }) 
 
    .then((response) => { 
 
     // you have access to params here 
 
     console.log(params.a); 
 
     //do stuffs 
 
    }) 
 
    .catch((error) => { 
 
     // you have access to params here 
 
     console.log('error', error.message); 
 
     console.log('params!!', params); 
 
     // need params._id here ?!! 
 
    }) 
 
} 
 

 
myWrapper();
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.17.1/axios.js"></script>

+0

問題は、私は別のpramsで複数回要求を行うことです、私は第二の要求が完了する前に最初のもののparamsをオーバーライドするのではないかと恐れていますか? –

+0

関数にパラメータを渡します。各呼び出しは新しいコンテキストを取得します –

+0

ああ、ありがとう –

関連する問題