首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >vue cli axios在获取请求时混合数组索引

vue cli axios在获取请求时混合数组索引
EN

Stack Overflow用户
提问于 2021-02-17 21:27:38
回答 1查看 27关注 0票数 0
代码语言:javascript
复制
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个不同的apis的翻译器。当我将英文代置推送到数组时,不能将土耳其语代置推送到另一个相同索引号的数组。我怎么才能修复它?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-17 21:39:54

我不知道为什么,但是axios做了另一个循环selfly和混合数组索引。例如:

代码语言:javascript
复制
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中的数组中。喜欢;

代码语言:javascript
复制
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
                })
          })
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66242864

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档