当前位置:   article > 正文

vue2/Vue3项目中,通过请求接口来刷新列表中的某个字段(如:Axios)_上述代码中,fetchdata 方法用于发送请求获取列表数据,将返回的数据存储在 list 数

上述代码中,fetchdata 方法用于发送请求获取列表数据,将返回的数据存储在 list 数

vue2/Vue3项目中,通过请求接口来刷新列表中的某个字段。可以使用 Vue 的异步请求库(如 Axios)来发送请求,并在请求成功后更新相应的字段。

  1. 示例如下(Vue2):

简单的示例如下,假设列表数据存储在 list 数组中,每个对象都有一个字段 field 需要刷新。示例代码如下:

<template>
  <div>
    <ul>
      <li v-for="item in list" :key="item.id">
        {{ item.field }}
        <button @click="refreshField(item.id)">刷新</button>
      </li>
    </ul>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      list: []
    };
  },
  methods: {
    fetchData() {
      // 发送请求获取列表数据(示例接口地址)
      axios.get('/api/list')
        .then(response => {
          this.list = response.data;
        })
        .catch(error => {
          console.error(error);
        });
    },
    refreshField(itemId) {
      // 发送请求刷新字段(示例接口地址)
      axios.put(`/api/item/${itemId}/refresh`)
        .then(response => {
          // 更新列表中对应项的字段
          const item = this.list.find(item => item.id === itemId);
          if (item) {
            item.field = response.data.field;
          }
        })
        .catch(error => {
          console.error(error);
        });
    }
  },
  mounted() {
    this.fetchData();
  }
};
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

上述代码中,fetchData 方法用于发送请求获取列表数据,将返回的数据存储在 list 数组中。每个列表项都有一个刷新按钮,点击按钮时会调用 refreshField 方法发送请求来刷新对应项的字段。在请求成功后,通过更新 list 数组中对应项的字段来实现刷新。

  1. 示例如下(Vue3):

Vue 3 中,可以使用 Composition API (组合式API)来编写相应的代码。下面是使用 Vue 3 和 Composition API 的示例代码:

<template>
  <div>
    <ul>
      <li v-for="item in list" :key="item.id">
        {{ item.field }}
        <button @click="refreshField(item.id)">刷新</button>
      </li>
    </ul>
  </div>
</template>

<script>
import { ref } from 'vue';
import axios from 'axios';

export default {
  setup() {
    const list = ref([]);

    const fetchData = () => {
      // 发送请求获取列表数据
      axios.get('/api/list')
        .then(response => {
          list.value = response.data;
        })
        .catch(error => {
          console.error(error);
        });
    };

    const refreshField = (itemId) => {
      // 发送请求刷新字段
      axios.put(`/api/item/${itemId}/refresh`)
        .then(response => {
          // 更新列表中对应项的字段
          const item = list.value.find(item => item.id === itemId);
          if (item) {
            item.field = response.data.field;
          }
        })
        .catch(error => {
          console.error(error);
        });
    };

    fetchData();

    return {
      list,
      refreshField
    };
  }
};
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

在这个示例中,我们使用了 ref 函数来创建响应式的 list 数组。fetchData 函数和 refreshField 函数被定义在 setup 函数中,并通过 return 导出,使它们能在模板中使用。

注意:由于 Vue 3 使用了 Composition API,所以代码中不再使用 datamethods 等选项,而是使用了函数式的 setup。在 setup 函数中,我们定义了需要用到的数据和方法,并将它们返回以在模板中使用。

可根据实际需求进行适当的修改和调整,仅提供思路~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/440951
推荐阅读
相关标签
  

闽ICP备14008679号