Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 多个v-Bind-子组件-API调用-VueJ_Javascript_Api_Data Binding_Components_Vuejs3 - Fatal编程技术网

Javascript 多个v-Bind-子组件-API调用-VueJ

Javascript 多个v-Bind-子组件-API调用-VueJ,javascript,api,data-binding,components,vuejs3,Javascript,Api,Data Binding,Components,Vuejs3,(我是Vuejs和API处理的初学者) 我目前正在使用加密货币API,并尝试调用一个属性(从一个子组件创建一个列表),向我展示硬币在24小时/7天/30天内的演变。 问题是,为了访问包含这些对象和属性的数据,我必须调用不同的url: 24小时: 7d: 30d: 然而,由于我必须对这些URL发出get请求,以便每次收集并存储其数据,这意味着我有3个不同的位置存储每个信息。 我能够从第一个绑定数据并将其发送给孩子,但也希望为其他两个绑定数据。 我尝试了许多方法,但找不到一种方法来进行多个绑定(

(我是Vuejs和API处理的初学者) 我目前正在使用加密货币API,并尝试调用一个属性(从一个子组件创建一个列表),向我展示硬币在24小时/7天/30天内的演变。 问题是,为了访问包含这些对象和属性的数据,我必须调用不同的url:

  • 24小时:
  • 7d:
  • 30d:
然而,由于我必须对这些URL发出get请求,以便每次收集并存储其数据,这意味着我有3个不同的位置存储每个信息。 我能够从第一个绑定数据并将其发送给孩子,但也希望为其他两个绑定数据。 我尝试了许多方法,但找不到一种方法来进行多个绑定(每个绑定一个:24小时/7天/30天)

以下是我当前的代码:

const proxyUrl = "https://cors-anywhere.herokuapp.com/"

const coins_url = "https://api.coinranking.com/v2/coins"
const coins_url_7d = "https://api.coinranking.com/v2/coins?timePeriod=7d"
const coins_url_30d = "https://api.coinranking.com/v2/coins?timePeriod=30d"


const access_token = 'hidden content'

const reqHeaders = {
    headers: {
        'Access-Control-Allow-Headers': 'x-access-token',
        'x-access-token': `token ${access_token}`
    }
}

export default {
    name: 'HomeResults',
    data(){
        return {
            limit: 5,
            cryptoList: [],
            detail: null,
            globVol24: null,
            totalCoins: null,
            marketCap: null,
            evol7d: null,
            evol30d: null
        }
    },
    mounted(){
        //COINS DATA
        axios
        .get(proxyUrl + coins_url, { 
            reqHeaders
        })
        .then((reponseCoins) => {
            // console.log(reponseCoins.data)
            //COINS DATA: this is where I store the first data (24h)
            this.cryptoList = reponseCoins.data.data.coins;
        })
        .catch((error) => {
            console.error(error)
        })


        // EVOLUTION 7 DAYS
        axios
        .get(proxyUrl + coins_url_7d, { 
            reqHeaders
        })
        .then((reponse7d) => {
            console.log(reponse7d.data)
           // this is where I store the first data (7d)
            this.evol7d = reponse7d.data.data.coins;
        })
        .catch((error) => {
            console.error(error)
        })

        // EVOLUTION 30 DAYS
        axios
        .get(proxyUrl + coins_url_30d, { 
            reqHeaders
        })
        .then((reponse30d) => {
            console.log(reponse30d.data)
            // this is where I store the first data (30d)
            this.evol30d = reponse30d.data.data.coins;
        })
        .catch((error) => {
            console.error(error)
        }),
       computed: {
        // Limiting results showing
        limitCryptoRes: function() {
            return this.limit ? this.cryptoList.slice(0, this.limit) : this.cryptoList;
        }
      },
      components: {
        'cryptoItem' : CryptoItem
      }
}
然后我将数据绑定到我的组件:

<tr v-bind:key="index" v-for="(detail, index) in limitCryptoRes">
    <cryptoItem 
     v-bind:cryptoList="detail"
     class="mt-1 mb-1 pt-3 pb-3 pl-3 pr-3"></cryptoItem>
</tr>

我尝试了几种绑定
cryptoList
evol7d
evol30d
的方法,但都没有成功。 很明显我在这里遗漏了一些东西

如果你需要额外的信息,不要犹豫


提前感谢您的帮助

您正在尝试迭代从
加密列表
计算的
limitCryptoRes
。但是向
加密列表
添加数据的唯一位置是第一个get请求

您需要将这些请求中的每个响应推送到
加密列表
数组,如下所示:

axios.get(proxyUrl + coins_url, { reqHeaders })
  .then((responseCoins) => {
    this.cryptoList.push(reponseCoins.data.data.coins)
  })
  .catch((err) => {
    console.error(err)
  })

您正在尝试迭代从
加密列表
计算的
limitCryptoRes
。但是向
加密列表
添加数据的唯一位置是第一个get请求

您需要将这些请求中的每个响应推送到
加密列表
数组,如下所示:

axios.get(proxyUrl + coins_url, { reqHeaders })
  .then((responseCoins) => {
    this.cryptoList.push(reponseCoins.data.data.coins)
  })
  .catch((err) => {
    console.error(err)
  })

我在codesandbox上分享了一些代码,作为一个实例


加密
{{crypto.name}
7天-24小时容量
{{`${crypto.name}:${crypto[“24hVolume”]}`}
30天-价格
{{`${crypto.name}:${crypto.price}`}
{{JSON.stringify(limitCryptoRes.list,null,2)}
7d结果
{{JSON.stringify(limitCryptoRes.evol7d,null,2)}
30d结果
{{JSON.stringify(limitCryptoRes.evol30d,null,2)}
从“axios”导入axios;
常量proxyUrl=”https://cors-anywhere.herokuapp.com/";
const coins_url=”https://api.coinranking.com/v2/coins";
const coins_url_7d=”https://api.coinranking.com/v2/coins?timePeriod=7d";
const coins\u url\u 30d=”https://api.coinranking.com/v2/coins?timePeriod=30d";
const access_token=“隐藏内容”;
常数reqHeaders={
标题:{
“访问控制允许标头”:“x-Access-token”,
“x-access-token”:`token${access\u token}`,
},
};
导出默认值{
名称:“HomeResults”,
数据(){
返回{
限额:5,
加密列表:{
名单:[],
evol7d:[],
evol30d:[],
},
详细信息:空,
globVol24:null,
totalCoins:null,
marketCap:null,
evol7d:null,
evol30d:null,
};
},
计算:{
limitCryptoRes:函数(){
返回{
list:this.cryptoList.list.slice(0,this.limit),
evol7d:this.cryptoList.evol7d.slice(0,this.limit),
evol30d:this.cryptoList.evol30d.slice(0,this.limit),
};
},
},
安装的(){
//硬币数据
axios
.get(proxyUrl+coins\u url{
请求标题,
})
.然后((回复硬币)=>{
//console.log(reponseconds.data)
//硬币数据:这是我存储第一个数据的地方(24小时)
this.cryptoList.list=reponseconds.data.data.coins;
})
.catch((错误)=>{
控制台错误(error);
});
//进化7天
axios
.get(proxyUrl+coins\u url\u 7d{
请求标题,
})
.然后((响应7d)=>{
console.log(reponse7d.data);
//这是我存储第一个数据的地方(7d)
this.cryptoList.evol7d=reponse7d.data.data.coins;
})
.catch((错误)=>{
控制台错误(error);
});
//进化30天
axios
.get(proxyUrl+coins\u url\u 30d{
请求标题,
})
.然后((回复30d)=>{
console.log(reponse30d.data);
//这是我存储第一个数据的地方(30d)
this.cryptoList.evol30d=reponse30d.data.data.coins;
})
.catch((错误)=>{
控制台错误(error);
});
},
};
#应用程序{
字体系列:Avenir、Helvetica、Arial、无衬线字体;
-webkit字体平滑:抗锯齿;
-moz osx字体平滑:灰度;
颜色:#2c3e50;
边缘顶部:60像素;
}
前{
文本对齐:左对齐;
背景颜色:浅灰色;
}
标签{
显示:块;
}

我在codesandbox上分享了一些代码,作为一个实例


加密
{{crypto.name}
7天-24小时容量
{{`${crypto.name}:${crypto[“24hVolume”]}`}
30天-价格
{{`${crypto.name}:${crypto.price}`}
{{JSON.stringify(limitCryptoRes.list,null,2)}
7d结果
{{JSON.stringify(limitCryptoRes.evol7d,null,2)}
30d结果
{{JSON.stringify(limitCryptoRes.evol30d,null,2)}
从“axios”导入axios;
常量proxyUrl=”https://cors-anywhere.herokuapp.com/";
const coins_url=”https://api.coinranking.com/v2/coins";
const coins_url_7d=”https://api.coinranking.com/v2/coins?timePeriod=7d";
const coins\u url\u 30d=”https://api.coinranking.com/v2/coins?timePeriod=30d";
const access_token=“隐藏内容”;
常数reqHeaders={
标题:{
“访问控制允许标头”:“x-Access-token”,
“x-access-token”:`token${access\u token}`,
},
};
导出默认值{
名称:“HomeResults”,
数据(){
返回{
限额:5,
加密列表:{
名单:[],
埃沃