невозможно записать метод API в data () vue - PullRequest
0 голосов
/ 09 января 2020

Я хочу, чтобы USD был взят из API, какая функция у меня в methods, но как я могу написать это?

  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)
      })
    }

1 Ответ

2 голосов
/ 09 января 2020

Это не то, как 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);
                });
        }
    }
}
...