Todos 10
这节实现三个按钮样式切换,使用三目运算即可实现。
<a-button type="primary / default">
需要注意的是 type 要进行属性绑定
(不要跟我一样败在一个冒号上)
1、store/index.js——增加 changeViewkey() 修改视图的关键字,增加公共数据 viewKey 存储当前选中按钮。
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
list: [],
inputValue: '', // 文本框内容
nextId: 5, // list中id属性取值
viewKey: 'all' // 当前选中按钮
},
mutations: {
initList (state, step) {
state.list = step
},
// 为 inputValue 赋值
setInputValue (state, val) {
state.inputValue = val
},
// 将inputValue存储到list中
addInputValue (state) {
const obj = {
id: state.nextId,
info: state.inputValue.trim(),
done: false
}
state.list.push(obj)
state.nextId++
state.inputValue = ''
},
removeItem1 (state, step) {
state.list.forEach((e, index) => {
if (e.id === step) {
return state.list.splice(index, 1)
}
})
},
removeItem2 (state, step) {
// 根据id查找对应的索引
const i = state.list.findIndex(x => x.id === step)
// 根据索引,删除对应的元素
if (i !== -1) {
state.list.splice(i, 1)
}
},
// 修改列表项的选中状态
changeStatus (state, param) {
const i = state.list.findIndex(x => x.id === param.id)
if (i !== -1) {
state.list[i].done = param.done
}
},
// 清除已完成列表项
listClean (state) {
state.list = state.list.filter(x => x.done === false)
},
// 修改视图的关键字
changeViewkey (state, step) {
state.viewKey = step
}
},
actions: {
getList (context) {
axios.get('/list.json').then(({ data }) => {
console.log(data)
context.commit('initList', data)
})
}
},
getters: {
// 统计未完成任务条数
unDoneLength (state) {
return state.list.filter(x => x.done === false).length
}
}
})
2、App.vue——三个按钮分别绑定 changeList() 事件,type使用属性绑定动态更改样式。
<template>
<div id="app">
<a-input
placeholder="请输入任务"
class="my_ipt"
:value="inputValue"
@change="handleInputChange"
/>
<a-button type="primary" @click="addItemToList()">添加事项</a-button>
<a-list bordered :dataSource="list" class="dt_list">
<a-list-item slot="renderItem" slot-scope="item">
<!-- 复选框 -->
<a-checkbox :checked='item.done' @change='onChange($event, item.id)'>{{ item.info }}</a-checkbox>
<!-- 删除链接 -->
<a slot="actions" @click="removeItemById(item.id)">删除</a>
</a-list-item>
<!-- footer区域 -->
<div slot="footer" class="footer">
<!-- 未完成的任务个数 -->
<span>{{ unDoneLength }}条剩余</span>
<!-- 操作按钮 -->
<a-button-group>
<a-button :type="viewKey === 'all' ? 'primary' : 'default'" @click="changeList('all')">全部</a-button>
<a-button :type="viewKey === 'unDone' ? 'primary' : 'default'" @click="changeList('unDone')">未完成</a-button>
<a-button :type="viewKey === 'done' ? 'primary' : 'default'" @click="changeList('done')">已完成</a-button>
</a-button-group>
<!-- 把已经完成的任务清空 -->
<a @click="cleanDone()">清除已完成</a>
</div>
</a-list>
</div>
</template>
<script>
import { mapState, mapMutations, mapGetters } from 'vuex'
export default {
name: 'app',
data () {
return {}
},
created () {
this.$store.dispatch('getList')
},
computed: {
...mapState(['list', 'inputValue', 'viewKey']),
...mapGetters(['unDoneLength'])
},
methods: {
...mapMutations(['setInputValue', 'addInputValue', 'removeItem1', 'removeItem2', 'changeStatus', 'listClean', 'changeViewkey']),
// 监听文本框内容变化
handleInputChange (e) {
// 拿到最新值,并同步到store
console.log(e.target.value)
this.setInputValue(e.target.value)
},
// 向列表项中新增 item 项
addItemToList () {
if (this.inputValue.trim().length <= 0) { // 判空处理
return this.$message.warning('文本框内容不能为空')
}
this.addInputValue()
},
removeItemById (id) {
this.removeItem1(id)
},
// 监听复选框选中状态的变化
onChange (e, id) {
// 通过 e.target.checked 可以接受到最新的选中状态
const param = {
id: id,
done: e.target.checked
}
this.changeStatus(param)
// 另一种写法:
// @change='(e) => {onChange(e, item.id)}'
// console.log(`checked = ${e.target.checked}`)
},
// 清除已完成绑定事件
cleanDone () {
this.listClean()
},
// 修改页面中展示的列表项
changeList (key) {
this.changeViewkey(key)
}
}
}
</script>
<style scoped>
#app {
padding: 10px;
}
.my_ipt {
width: 500px;
margin-right: 10px;
}
.dt_list {
width: 500px;
margin-top: 10px;
}
.footer {
display: flex;
justify-content: space-between;
align-items: center;
}
</style>
另外
记得常常练习我们项目【实践篇】开端讲到的git指令,及时记录每个变动和版本,链接: Vuex入门到实战(六)—【实践篇】目标功能阐述+git指令,
git status / git add . / git commit -m “本次提交注释” / git log