【问题标题】:vue cli axios mixing array indexes when getting requestsvue cli axios在获取请求时混合数组索引
【发布时间】:2021-02-17 13:27:38
【问题描述】:
const sentenc = this.colocation[0][i].examples;
            sentenc.forEach((item,index) => {
            var colAction = this.colocation[0][i].examples[index];
            const strippedString = colAction.replace(/(<([^>]+)>)/gi, "");
 this.examplesEN.push(strippedString)
              axios.get('https://www.googleapis.com/language/translate/v2?key={apikey}='+strippedString+'&source=en&target=tr')
                .then((response) =>{
                  const exTranslet = response.data.data.translations[0].translatedText;
                  this.examplesTR.push(exTranslet);
                 
                })
          })

我正在使用 2 个不同的 API 构建翻译器。当我将英语托管推向阵列时,不能将土耳其托管推向另一个阵列以相同的索引号。我该如何解决?

【问题讨论】:

    标签: vue.js axios


    【解决方案1】:

    我不知道为什么,但是 axios 自己做另一个循环并混合数组索引。例如;

    for(i = 0; i<5; i++)
        {
          //your axios codes
           {
              //i = 0
              //i = 3
              //i = 2
              //i = 4
              //i = 1
           }
       //i = 0
       //i = 1
       //i = 2
       //i = 3
       //i = 4
        }
    

    您应该将所有数据推送到 axios 中的数组。喜欢;

    const sentenc = this.colocation[0][i].examples;
                sentenc.forEach((item,index) => {
                var colAction = this.colocation[0][i].examples[index];
                const strippedString = colAction.replace(/(<([^>]+)>)/gi, "");
                  axios.get('https://www.googleapis.com/language/translate/v2?key={apikey}='+strippedString+'&source=en&target=tr')
                    .then((response) =>{
                      const exTranslet = response.data.data.translations[0].translatedText;
                      this.examplesTR.push(exTranslet);
                      this.examplesEN.push(strippedString) //use this line in axios
                    })
              })
    

    【讨论】:

      猜你喜欢
      • 2020-01-03
      • 1970-01-01
      • 2019-11-22
      • 2020-02-14
      • 1970-01-01
      • 2021-12-29
      • 2020-07-17
      • 1970-01-01
      • 2020-09-03
      相关资源
      最近更新 更多