Element-ui框架完成vue2项目的vuex的增删改查

看效果图是否是你需要的

这是原来没有Element-ui框架的

首先,你要在你的项目里安装Element-ui

yarn命令

复制代码
 yarn add element-ui

npm命令

复制代码
npm install element-ui --save

好了现在可以粘贴代码

复制代码
//main.js
import Vue from 'vue'
import Vuex from 'vuex'
import VueRouter from 'vue-router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import store from './tool/store'
import App from './App.vue'
import router from '@/tool/router'

Vue.use(Vuex)
Vue.use(VueRouter)
Vue.use(ElementUI)

new Vue({
    el: '#app',
    router,
    store,
    render: h => h(App)
})

//store.js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        goodsList: []
    },
    mutations: {
        addGood(state, { name, price }) {
            state.goodsList.push({ id: state.goodsList.length + 1, name, price })
        },
        delGood(state, id) {
            const index = state.goodsList.findIndex(item => item.id === id)
            if (index !== -1) {
                state.goodsList.splice(index, 1)
            }
        },
        updateGood(state, { id, name, price }) {
            const good = state.goodsList.find(item => item.id === id)
            if (good) {
                good.name = name
                good.price = price
            }
        }
    }
})

//router.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import EditStore from '@/components/EditStore.vue'
import IndexStore from '@/components/IndexStore.vue'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/index'
    },
    {
        path: '/index',
        component: IndexStore
    },
    {
        path: '/edit/:id',
        component: EditStore,
        name: 'Edit',
        props: true
    }
]

const router = new VueRouter({
    routes
})

export default router

<!-- EditStore.vue -->
<template>
  <div>
    <h3>编辑商品</h3>
    <el-form :model="form" label-width="80px">
      <el-form-item label="商品名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="商品价格">
        <el-input-number v-model="form.price" controls-position="right" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: ['id'],
  computed: {
    goodInfo() {
      return this.$store.state.goodsList.find(item => item.id === this.id)
    }
  },
  data() {
    return {
      form: {
        name: '',
        price: 0
      }
    }
  },
  mounted() {
    this.form = {
      name: this.goodInfo.name,
      price: this.goodInfo.price
    }
  },
  methods: {
    submit() {
      this.$store.commit({
        type: 'updateGood',
        id: this.id,
        name: this.form.name,
        price: this.form.price
      })
      this.$router.push('/index')
    }
  }
}
</script>

<!-- IndexStore.vue -->
<template>
  <div>
    <h3 style="margin-left: 200px">商品列表</h3>
    <el-table :data="goodsList" border style="width: 600px; margin-left: 200px">
      <el-table-column prop="name" label="商品名称"></el-table-column>
      <el-table-column prop="price" label="价格"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" @click="delGood(scope.row.id)">删除</el-button>
          <el-button type="primary" size="mini" @click="editGood(scope.row.id)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button style="margin-left: 200px; margin-top: 10px" type="primary" @click="add">添加商品</el-button>
  </div>
</template>

<script>
export default {
  name: 'IndexStore',
  computed: {
    goodsList() {
      return this.$store.state.goodsList;
    }
  },
  methods: {
    add() {
      const { name, price } = this.randomGood()
      this.$store.commit({ type: 'addGood', name, price })
    },
    randomGood() {
      const goods = [
        { name: '洗衣机', price: 990 },
        { name: '油烟机', price: 2239 },
        { name: '电饭煲', price: 200 },
        { name: '电视机', price: 880 },
        { name: '电冰箱', price: 650 },
        { name: '电脑', price: 4032 },
        { name: '电磁炉', price: 210 }
      ]
      return goods[parseInt(Math.random() * 7)]
    },
    delGood(id) {
      this.$store.commit('delGood', id)
    },
    editGood(id) {
      this.$router.push({ name: 'Edit', params: { id } })
    }
  }
}
</script>
相关推荐
开开心心就好5 小时前
批量提取PDF中的图片,直接导出原图
前端·javascript·支持向量机·智能手机·pdf·html·启发式算法
Setsuna_F_Seiei7 小时前
前端转型 Agent 开发 05 之 Agent Hooks 与 Checkpointer(让 Agent 从全自动转变人为可掌控)
前端·agent·ai编程
百万蹄蹄向前冲9 小时前
风扇转了一晚上MVP专家团翻车事故
前端·人工智能
默_笙9 小时前
🏛 给 AI 配一间办公室:Harness Engineering 六大模块与它的实现
前端·javascript
linux_cfan10 小时前
videojs v10 源代码系列解读:14 · 谓词守卫:在运行时安全地调用能力
前端·javascript·音视频
kyriewen10 小时前
我扒了 10,221 条 JD:腾讯技术岗 75% 在要 AI
前端·人工智能·ai编程
郑州光合科技余经理11 小时前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
+VX:Fegn089512 小时前
计算机毕业设计|基于springboot + vue旅游管理系统(源码+数据库+文档)
java·开发语言·vue.js·spring boot·课程设计
雪芽蓝域zzs12 小时前
第 7 章:双 Y 轴组合图(柱状 + 折线,看板高频场景)
vue.js·echars
IT_陈寒12 小时前
Vue的响应式让我熬到凌晨三点,原来漏了这个小细节
前端·人工智能·后端