2017-06-19 67 views
0

我正在为我的加密货币购买自己的目录。Vue.js根变量

我通过API获取BTC,ETH和LTC的价格,然后我为每个打出的硬币创建了一个组件,然后我想计算当前价格(ownedCoins * currentPrice)。

所以在我的$根我有{乙:324.233,BTC:2211.43,LTC:41.341}

这里就是我想计算的话:

self.eur = response.data.sum[0].quantity * this.$root.ltc; 

但我想使这个动态的,所以我想要做的是创建一个动态变量。类似的东西:self.eur = response.data.sum[0].quantity * this.$root.{this.coinName};

我该怎么做?

+0

除了self.eur本身,哪个值应该在数据中动态变化? – Cobaltway

+0

请注意'response.data.sum [0] .quantity * this。$ root。{this.coinName}'是无效的js。 'response.data.sum [0] .quantity * this。$ root [this.coinName]'is。 – Cobaltway

回答

1

我会读取VueJS文档的State Management部分则检出Vuex docs.一旦你的数据存储获得甚至轻度更复杂的与你的示例代码管理将成为压倒性的方法。

0

你的问题与vue没有任何关系,只是简单的javascript。使用在JavaScript中你有2种方式的对象变量,使用点符号或括号标记(我把它称为数组符号):

const car = { wheels: 4, seats: 5, horsepower: 145 }; 

console.log(car.wheels); 
console.log(car['wheels']); //same result 

所以

this.$root[this.coinName]; 

会给你你正在寻找的结果。