vuex state中的数组变化监听实例

yizhihongxing

关于Vuex state中的数组变化监听,可以使用Vue提供的watch方法监听数组变化。

先来简单介绍一下实现的方法:

  1. 在state定义的数组数据前加上$符号,例如:$list

  2. 监听数据的方式:

    // 监听数据变化
    watch: {
        '$store.state.list': {
            handler: function (newValue, oldValue) {
                console.log('watch state changed:', newValue, oldValue);
            },
            deep: true
        }
    }

这里需要注意几点:

  1. 监听数组数据时需要使用deep:true,因为Vue默认只是浅层监测,只有在值为基本数据类型时才能被监测到,而不是对象或者数组;

  2. 这里使用的是Vue中的$watch方法,而不是Vuex中的watch方法,因为Vuex中的watch方法只能处理同步操作。

下面提供两个示例:

一. 会计记录表

// state
const state = {
  $records: [
    {id: 1, name: '张三', amount: 100},
    {id: 2, name: '李四', amount: 200},
    {id: 3, name: '王五', amount: 50},
  ]
}

// getters
const getters = {}

// mutations
const mutations = {
  addRecord(state, record) {
    // 新增记录
    state.$records.push(record)
  },
  deleteRecord(state, id) {
    // 删除记录
    const index = state.$records.findIndex(record => record.id === id)
    state.$records.splice(index, 1)
  },
  updateRecord(state, record) {
    // 更新记录
    const index = state.$records.findIndex(r => r.id === record.id)
    state.$records.splice(index, 1, record)
  },
}

// 记录列表
Vue.component('record-list', {
  template: `
    <div>
      <table>
        <thead>
          <tr>
            <th>ID</th><th>姓名</th><th>金额</th><th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="record in $records" :key="record.id">
            <td>{{ record.id }}</td>
            <td>{{ record.name }}</td>
            <td>{{ record.amount }}</td>
            <td>
              <button @click="edit(record)">编辑</button>
              <button @click="deleteRecord(record.id)">删除</button>
            </td>
          </tr>
        </tbody>
      </table>
      <button @click="add">新增</button>
    </div>
  `,
  data() {
    return {
      record: {}
    }
  },
  computed: {
    $records() {
      return this.$store.state.$records
    }
  },
  methods: {
    add() {
      this.record = {}
      this.$store.commit('addRecord', this.record)
    },
    edit(record) {
      this.record = Object.assign({}, record)
    },
    deleteRecord(id) {
      this.$store.commit('deleteRecord', id)
    }
  },
  // 监听数据
  watch: {
    '$store.state.$records': {
      handler: function (newValue, oldValue) {
        console.log('watch state changed:', newValue, oldValue);
      },
      deep: true,
    }
  }
})

这是一个会计记录表的示例,其中state中包含一个$records数组,该数组包含所有的会计记录。组件中定义了记录列表,并提供了和该表格相关的操作,包括新增、编辑和删除记录。同时使用watch方法来监测到该列表的变化。

二. 订单列表

// state
const state = {
  $orders: [
    {id: 1, name: '订单1', products: ['产品1', '产品2']},
    {id: 2, name: '订单2', products: ['产品3', '产品4']},
    {id: 3, name: '订单3', products: ['产品5', '产品6']},
  ]
}

// getters
const getters = {}

// mutations
const mutations = {
  addOrder(state, order) {
    // 新增订单
    state.$orders.push(order)
  },
  deleteOrder(state, id) {
    // 删除订单
    const index = state.$orders.findIndex(order => order.id === id)
    state.$orders.splice(index, 1)
  },
  updateOrder(state, order) {
    // 更新订单
    const index = state.$orders.findIndex(o => o.id === order.id)
    state.$orders.splice(index, 1, order)
  },
}

// 订单列表
Vue.component('order-list', {
  template: `
    <div>
      <table>
        <thead>
          <tr>
            <th>ID</th><th>名称</th><th>产品列表</th><th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="order in $orders" :key="order.id">
            <td>{{ order.id }}</td>
            <td>{{ order.name }}</td>
            <td>{{ order.products.join(', ') }}</td>
            <td>
              <button @click="edit(order)">编辑</button>
              <button @click="deleteOrder(order.id)">删除</button>
            </td>
          </tr>
        </tbody>
      </table>
      <button @click="add">新增</button>
    </div>
  `,
  data() {
    return {
      order: {}
    }
  },
  computed: {
    $orders() {
      return this.$store.state.$orders
    }
  },
  methods: {
    add() {
      this.order = {}
      this.$store.commit('addOrder', this.order)
    },
    edit(order) {
      this.order = Object.assign({}, order)
    },
    deleteOrder(id) {
      this.$store.commit('deleteOrder', id)
    }
  },
  // 监听数据
  watch: {
    '$store.state.$orders': {
      handler: function (newValue, oldValue) {
        console.log('watch state changed:', newValue, oldValue);
      },
      deep: true,
    }
  }
})

这是一个订单列表的示例,其中state中包含一个$orders数组,该数组包含所有的订单信息,包括名称和产品列表。组件中定义了订单列表,并提供了常见的订单操作,包括新增、编辑和删除订单信息,同时也使用watch方法来监测到该列表的变化。

以上就是Vuex state中的数组变化监听实例的详细攻略,希望能够对您有所帮助。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:vuex state中的数组变化监听实例 - Python技术站

(0)
上一篇 2023年5月29日
下一篇 2023年5月29日

相关文章

  • vue大型项目之分模块运行/打包的实现

    要将Vue大型项目分模块运行/打包,一般需要使用Vue的路由功能和Webpack的代码分割功能。 使用Vue路由功能 Vue路由功能可以帮助我们在不同的URL路径中渲染不同的组件。这是实现分模块运行的重要前提。 首先,我们需要在项目中安装vue-router库: npm install vue-router –save 接下来,在Vue实例中使用vue-r…

    Vue 2023年5月27日
    00
  • vue项目如何实现前端预览word与pdf格式文件

    要实现前端预览word与pdf格式文件,我们需要借助一些第三方库或工具。以下是一些实现前端预览word与pdf格式文件的常见方法: 1. 使用第三方库进行预览 我们可以使用一些第三方库来实现前端预览word与pdf格式文件,例如viewerjs和pdf.js。 使用viewerjs Viewerjs是一个用于在网页上预览office文档和pdf文件的开源库。…

    Vue 2023年5月28日
    00
  • 10分钟上手vue-cli 3.0 入门介绍

    10分钟上手vue-cli 3.0 入门介绍 什么是vue-cli 3.0 vue-cli是一套通过命令行工具帮助我们快速创建Vue.js项目开发环境的脚手架工具。其3.0版本带来了更好的用户体验和更快的构建速度。 安装vue-cli 3.0 首先,我们需要安装node.js和npm。安装完毕后,打开命令行窗口,输入以下命令进行全局安装vue-cli: np…

    Vue 2023年5月27日
    00
  • 基于SpringBoot和Vue3的博客平台发布、编辑、删除文章功能实现

    下面我将详细讲解如何基于SpringBoot和Vue3搭建一个简单的博客平台,并实现发布、编辑和删除文章的功能。 准备工作 首先,我们需要搭建开发环境,并且安装必要的工具和依赖。具体的步骤如下: 安装Java SDK:前往 https://www.oracle.com/java/technologies/javase-downloads.html 下载并安装…

    Vue 2023年5月27日
    00
  • vue3中defineComponent 的作用详解

    下面就是对“vue3中defineComponent 的作用详解”的完整攻略: 什么是 defineComponent defineComponent 是 Vue 3 中的一个工厂函数,用来创建一个组件。我们可以将其看作是一个替代 Vue 2.x 中的 Vue.extend 和单文件组件中的 export default。 defineComponent 可…

    Vue 2023年5月27日
    00
  • 使用vue写一个翻页的时间插件实例代码

    下面我为您详细讲解如何使用vue写一个翻页的时间插件实例代码。 1. 创建vue组件 首先,在Vue项目中定义一个翻页的时间插件组件。为了便于管理,我们通常将该组件定义在一个单独的文件中,在该文件中定义一个名为Timer.vue的组件。 <template> <div class="timer"> <span…

    Vue 2023年5月29日
    00
  • Java 实现简单静态资源Web服务器的示例

    实现一个简单的静态资源Web服务器,可以基于Java语言编写。本文将提供一个完整的攻略,方便初学者快速上手。 1 创建项目 首先需要创建一个Java项目,可以使用Eclipse或者其他IDE。创建项目后,需要创建如下的目录结构: src ├── main │ └── java │ └── com │ └── example │ └── webserver │…

    Vue 2023年5月28日
    00
  • Vue实现模糊查询filter()实例详解

    Vue实现模糊查询filter()实例详解 1. 简介 在Vue中,我们可以通过实现filter()函数来实现文本框的模糊查询功能,用户可以输入关键字,然后Vue会根据关键字对数据源进行过滤,只展示符合要求的数据,这无疑会提高应用程序的用户体验,本文就是要介绍如何使用Vue实现模糊查询filter()函数的详细攻略。 2. 实现步骤 2.1 准备数据 首先,…

    Vue 2023年5月27日
    00
合作推广
合作推广
分享本页
返回顶部