【问题标题】:can't write api method in data() vue无法在 data() vue 中编写 api 方法
【发布时间】:2020-01-08 21:11:55
【问题描述】:

我希望从我在methods中的哪个函数的api中获取美元,但我该怎么写呢?

  data(){
      return {
        posts: 1,
        USD:changeCurrency()
      }
  },
  methods: {
    changeCurrency: function () {
      axios.get('http://data.fixer.io/api/latest?access_key=509c9d50c1e92a712be9c8f1f964cf67')
      .then(response => {
        this.posts = response.data.rates.GEL.toFixed(3)
      })
    }

【问题讨论】:

  • 您如何使用您的费率?显示模板。

标签: vue.js axios


【解决方案1】:

data 不应该这样使用。

您可以在挂载或组件本身中调用changeCurrency @click="changeCurrency"

{
    data() {
        return {
            posts: 1,
            // USD:changeCurrency()
        };
    },
    mounted() {
        // you could call here instead
        this.changeCurrency();
    },
    methods: {
        changeCurrency: function () {
            axios.get('http://data.fixer.io/api/latest?access_key=509c9d50c1e92a712be9c8f1f964cf67')
                .then(response => {
                    this.posts = response.data.rates.GEL.toFixed(3);
                });
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2019-04-10
    • 2013-01-03
    相关资源
    最近更新 更多