美文网首页
4、vuex状态管理--图书增加、删除小案例

4、vuex状态管理--图书增加、删除小案例

作者: 蕉下客_661a | 来源:发表于2019-04-26 18:29 被阅读0次
GIF.gif
<template>
    <div class="myorder">
        <h1>Hello Vue!</h1>
        <h2>添加书籍</h2>
        <hr/>
        <p>书名:</p>
        <input type="text" v-model="title" class="bookTxt"/>
        <p>作者:</p>
        <input type="text" v-model="author" class="bookTxt"/>
        <p>价格:</p>
        <input type="text" v-model="price" class="bookTxt"/>
        <div><button class="add_book" @click="addBook">添加</button></div>
        <div>
            <table class="tab_book">
                <tr>
                    <th>序号</th>
                    <th>书名</th>
                    <th>作者</th>
                    <th>价格</th>
                    <th>操作</th>
                </tr>
                <tr  v-for="(book,index) in bookList" :key="index">
                    <td>{{book.id }}</td>
                    <td>{{book.title}}</td>
                    <td>{{book.author}}</td>
                    <td>{{book.price}}</td>
                    <td><a href="javascript:;" class="del_book" @click="delBook(index)">删除</a></td>
                </tr>
            </table>
            <div class="total_price">总价:<strong>{{getTotalPrice}}</strong></div>
        </div>       
    </div>
</template>
<script>
    import { mapState,mapGetters,mapMutations} from 'vuex'
    export default{
        computed:{
            ...mapState({
                bookList:state => state.manageStatus.bookList,
                totalPrice:state => state.manageStatus.totalPrice
            }),
            ...mapGetters(['getTotalPrice']),
            title:{
                get() {
                    return this.$store.state.manageStatus.newBook.title
                },
                set(title){
                    this.$store.commit('setBookTitle',title);
                }
            },
           author:{
                get() {
                    return this.$store.state.manageStatus.newBook.author
                },
                set(author){
                    this.$store.commit('setBookAuthor',author);
                }
            },
            price:{
                get() {
                    return this.$store.state.manageStatus.newBook.price
                },
                set(price){
                    this.$store.commit('setBookPrice',price);
                }
            }          
        },
        methods:{            
            ...mapMutations(['addBook','delBook'])
        }
    }
</script>
<style>
    .myorder{
        background:#f5f5f5;
        padding:100px 50px;
    }
    .bookTxt{
        width: 300px;
        padding:8px 10px;
        border-radius:4px;
        border:1px solid #e5e5e5;
        margin:5px 0px;
        box-sizing:border-box;
        background:#fff;
    }
    .add_book{
        display:inline-block;
        width:300px;
        padding:8px 10px;
        border-radius:4px;
        background:#337ab7;
        color:#fff;
        outline:none;
        border:none;
        margin-top:10px;
    }
    .tab_book{
        width:500px;
        border-collapse: collapse;
        margin-top:30px;
    }
    .tab_book tr{
        border-bottom:1px solid #999;
    }
    .tab_book td{
        text-align:center;
        padding:10px;
    }
    .del_book{
        display:inline-block;
        padding:6px 10px;
        border-radius:4px;
        background:#5cb85c;
        color:#fff;
    }
    .total_price{
        padding:10px 0px 10px 400px;
    }
   
</style>

vuex中 manageStatus.js模块 存放路径:store/modules/manageStatus.js

const state ={    
    bookList:[{
            id:1,
            title:'红楼梦',
            author:'曹雪芹',
            price:32
        },
        {
            id:2,
            title:'水浒传',
            author:'施耐庵',
            price:30
        },
        {
            id:3,
            title:'三国演义',
            author:'罗贯中',
            price:24
        },
        {
            id:4,
            title:'西游记',
            author:'吴承恩',
            price:20
        }     
    ],
    newBook:{
        id:0,
        title:'',
        author:'',
        price:0
    },
    totalPrice:0 
}
const getters = {
      getTotalPrice:state =>{
            state.totalPrice = 0;
            state.bookList.forEach(function(val){
                state.totalPrice += val.price;        
            })
            return state.totalPrice;
      }  
}
const mutations = {
    setBookTitle(state,title){
        state.newBook.title = title
    },
    setBookAuthor(state,author){
        state.newBook.author = author
    },
    setBookPrice(state,price){
        state.newBook.price = parseInt(price);
    },
    addBook(state){
        const len = state.bookList.length;
        state.newBook.id = len>0 ? (state.bookList[len-1].id +1):1;
        state.bookList.push(state.newBook);
        state.newBook={
            id:0,
            title:'',
            author:'',
            price:0
        }
        
    },
    delBook(state,index){
        state.bookList.splice(index,1);
    }
}
// const actions = {}

export default {
    state,
    getters,
    mutations,
    //actions
}

这是store 文件夹下的index.js

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

import manageStatus from './modules/manageStatus'


export default new Vuex.Store({
    modules:{
        manageStatus
    }
})

相关文章

  • 4、vuex状态管理--图书增加、删除小案例

    vuex中 manageStatus.js模块 存放路径:store/modules/manageStatus...

  • Vuex 状态管理

    Vue组件间通信方式 Vuex核心概念和基本使用 购物车案例 模拟实现vuex 一、组件内的状态管理流程 状态管理...

  • 图书管理小案例

    实现的功能:图书管理->查找 增加 删除 修改代码实现: 函数封装:

  • vue使用$root设置全局属性

    vue状态管理使用vuex,如果项目不大,逻辑不多,那么我们没必要用vuex给项目增加难度,只需要用$root设置...

  • vuex概念总结及简单使用实例

    原文 博客原文 大纲 1、什么是Vuex2、什么是“状态管理模式”?3、什么情况下应该使用 Vuex?4、Vuex...

  • Vuex状态管理模式

    Vuex是什么? Vuex是一个专为Vue.js应用程序开发的状态管理模式Vuex是全局的状态管理 Vuex用来做...

  • Vue知识总结(2)

    Vuex状态管理 Vuex是专门为Vue应用程序提供的状态管理模式,每个Vuex应用的核心是store(仓库),即...

  • vuex二次总结

    Vuex状态管理 每个Vuex应用的核心是store(仓库)Vuex是专门为Vue应用程序提供的状态管理模式,每个...

  • Vue2技术栈归纳与精粹(下篇)

    Vuex状态管理 Vuex是专门为Vue应用程序提供的状态管理模式,每个Vuex应用的核心是store(仓库),即...

  • vuex介绍

    1. vuex是什么 创建vue-cli 2. 状态自管理应用 3. 多组件共享状态的问题 4. vuex的核心概...

网友评论

      本文标题:4、vuex状态管理--图书增加、删除小案例

      本文链接:https://www.haomeiwen.com/subject/aewqnqtx.html