【问题标题】:How do I call Axios on prop change in Vue?如何在 Vue 中更改道具时调用 Axios?
【发布时间】:2019-01-12 17:41:33
【问题描述】:

在更改id 值时,我想通过 Axios 进行 JSON 调用并更新页面的必要部分。我怎么做?目前,我有mountedactivated,它们似乎没有工作......

代码:

const Home = { 
    template: `

    <div class="user">
        <h2>user {{ id }}</h2>
        <h2>{{ info }}</h2>
        bet
    </div>

    `,
    props: {
        id: {
            type:    String,
            default: 'N/A'
        }
    },
    data () {
        return {
          info: null
        }
      },
    activated () {
        axios
          .get('https://api.coindesk.com/v1/bpi/currentprice.json', 
          { params: { id: id }}
          )
          .then(response => (this.info = response))
      }, 

      mounted() {
        axios
          .get('https://api.coindesk.com/v1/bpi/currentprice.json')
          .then(response => (this.info = 'response'))
      } 
}`

【问题讨论】:

    标签: javascript vue.js axios


    【解决方案1】:

    你可以使用watch收听id的道具变化:

    watch: {
      id: function(newId) {
        axios
          .get('https://api.coindesk.com/v1/bpi/currentprice.json',
               { params: { id: newId }}
          )
          .then(response => (this.info = response))
      }
    }
    

    这是一个基于您共享的代码的小演示,它显示了 watch 如何对 id 属性更改做出反应。下面的Wrapper 组件仅用于演示目的,因为它会触发id 值更改。

    const Home = {
      template: `
    
        <div class="user">
            <h2>user {{ id }}</h2>
            <h2>{{ info }}</h2>
            bet
        </div>
    
        `,
      props: {
        id: {
          default: 'N/A'
        }
      },
      data () {
        return {
          info: null
        }
      },
      mounted() {
        axios
          .get('https://api.coindesk.com/v1/bpi/currentprice.json')
          .then(response => (this.info = 'response'))
      },
      watch: {
        id: function(newId) {
          console.log(`watch triggered, value of id is: ${newId}`);
          axios
            .get('https://api.coindesk.com/v1/bpi/currentprice.json',
              { params: { id: newId }}
            )
            .then(response => (this.info = response))
        }
      }
    }
    
    const Wrapper = {
      template: '<div><home :id="id" /></div>',
      components: { Home },
      data() {
         return {
            id: 0
         }
      },
      mounted() {
         const limit = 5;
         const loop = (nextId) => setTimeout(() => {
           console.log(`#${nextId} loop iteration`);
           if (nextId < limit) {
              this.id = nextId;
              loop(nextId + 1);
           }
         }, 3000);
         loop(this.id);
      }
    }
    
    new Vue({
      el: '#app',
      components: { Wrapper }
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js" ></script>
    <div id="app">
    <wrapper />
    </div>

    【讨论】:

    • 那会去哪里?对不起,我是 Vue 的新手 :') @antonku
    • @BryanSanders 没问题 :) 我已经更新了答案
    • 当我的{{ id }} 更新时,我没有看到它更新。我更希望能够从 'product/foo' 转到 '/product/facts' 并称之为 axios。那是什么?再次道歉哈哈!
    • 我明白了,您能检查一下id 属性更改时是否调用了watch 回调?
    • @BryanSanders 我已经通过使用watch 与您的组件的演示更新了答案。希望它会有所帮助。
    猜你喜欢
    • 2021-12-08
    • 2019-10-25
    • 1970-01-01
    • 2018-07-05
    • 2020-01-19
    • 2018-07-19
    • 2021-02-18
    • 2021-04-01
    • 1970-01-01
    相关资源
    最近更新 更多