Skip to content

Commit eb134e6

Browse files
committed
修复todos.vue无法使用time travel
1 parent 27350ab commit eb134e6

File tree

5 files changed

+37
-29
lines changed

5 files changed

+37
-29
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -968,11 +968,14 @@ methods: {
968968
969969
这一节教程中,我们基于`03.vue-todos`将把`vue`的状态管理插件`vuex`整合进来,复制项目`03.vue-todos`改名为`04.vue-vuex-todos`
970970
971-
加入vuex插件库
971+
加入vuex插件库和vuex-router-sync(把route路由的状态同步到vuex的state中,方便跟踪路由的状态)
972972
````
973973
cnpm i vuex --save
974+
cnpm i vuex-router-sync --save
974975
````
975976
977+
这一节不对vuex的api做深入解析,若遇到不理解的地方还请翻阅[官方文档](https://vuex.vuejs.org/zh-cn/)
978+
976979
为了不影响`lesson03`的`store`实现,我们在`src`中新建了一个`vuex-store`文件夹,作为vuex的store存储库
977980
`src/vuex-store/index.js`
978981
````javascript
@@ -983,6 +986,7 @@ import uuid from 'uuid'
983986
Vue.use(Vuex)
984987
985988
export default new Vuex.Store({
989+
strict: process.env.NODE_ENV !== 'production',
986990
state: {
987991
todos: [] // 存储所有todos
988992
},
@@ -1022,6 +1026,9 @@ export default new Vuex.Store({
10221026
})
10231027
````
10241028
1029+
`strict: process.env.NODE_ENV !== 'production'`
1030+
在严格模式下,无论何时发生了状态变更且不是由 mutation 函数引起的,将会抛出错误。这能保证所有的状态变更都能被调试工具跟踪到。
1031+
10251032
> Vuex 通过 store 选项,提供了一种机制将状态从根组件『注入』到每一个子组件中(需调用 Vue.use(Vuex))
10261033
10271034
src/main.js

lesson/04.vue-vuex-todos/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"uuid": "^3.0.1",
1818
"vue": "^2.2.2",
1919
"vue-router": "^2.2.0",
20-
"vuex": "^2.2.1"
20+
"vuex": "^2.2.1",
21+
"vuex-router-sync": "^4.1.2"
2122
},
2223
"devDependencies": {
2324
"autoprefixer": "^6.7.2",

lesson/04.vue-vuex-todos/src/components/Todos/Todos.vue

+21-24
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,49 @@
11
<template>
22
<section class="main">
3-
<input class="toggle-all" type="checkbox" >
3+
<input class="toggle-all" type="checkbox">
44
<ul class="todo-list">
5-
<li v-for="todo in filteredTodos"
6-
class="todo"
7-
:key="todo.id"
8-
:class="{ completed: todo.completed, editing: todo == editedTodo }">
5+
<li v-for="todo in filteredTodos" class="todo" :key="todo.id" :class="{ completed: todo.completed, editing: todo == editedTodo }">
96
<div class="view">
107
<input class="toggle" type="checkbox" v-model="todo.completed">
118
<label @dblclick="editTodo(todo)">{{ todo.title }}</label>
129
<button class="destroy" @click="removeTodo(todo)"></button>
1310
</div>
14-
<input class="edit" type="text"
15-
v-model="todo.title"
16-
v-todo-focus="todo == editedTodo"
17-
@blur="doneEdit(todo)"
18-
@keyup.enter="doneEdit(todo)"
11+
<input class="edit" type="text" v-model="todo.title" v-todo-focus="todo == editedTodo" @blur="doneEdit(todo)" @keyup.enter="doneEdit(todo)"
1912
@keyup.esc="cancelEdit(todo)">
2013
</li>
2114
</ul>
2215
</section>
2316
</template>
2417

2518
<script>
26-
import Store from '@/store/index'
27-
19+
// import Store from '@/store/index'
20+
// visibility filters
21+
const filters = {
22+
All: todos => todos,
23+
Active: todos => (todos.filter(todo => !todo.completed)),
24+
Completed: todos => (todos.filter(todo => todo.completed))
25+
}
2826
export default {
2927
name: 'todos',
3028
data () {
3129
return {
32-
filteredTodos: [], // 根据filter过滤后的todos
3330
editedTodo: null // 记录当前编辑的todo
3431
}
3532
},
36-
created () {
37-
this.filterTodos()
38-
},
39-
watch: {
40-
// 如果路由有变化,会再次执行该方法
41-
'$route': function (to) {
42-
Store.filter.save(to.params.filter)
43-
this.filterTodos()
33+
computed: {
34+
filteredTodos: function () {
35+
return this.filterTodos()
4436
}
4537
},
4638
methods: {
4739
filterTodos () {
48-
let filter = this.$route.params.filter
49-
if (!filter) return
50-
this.filteredTodos = this.$store.getters[filter]
40+
let filteredTodos = []
41+
let filter = this.$store.state.route.params.filter
42+
if (filter && filters[filter]) {
43+
let filterFun = filters[filter]
44+
filteredTodos = filterFun(this.$store.state.todos)
45+
}
46+
return filteredTodos
5147
},
5248
5349
editTodo: function (todo) {
@@ -84,4 +80,5 @@
8480
}
8581
}
8682
}
83+
8784
</script>

lesson/04.vue-vuex-todos/src/main.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// The Vue build version to load with the `import` command
22
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
33
import Vue from 'vue'
4+
import { sync } from 'vuex-router-sync'
45
import App from './App'
56
import router from './router'
67
import store from './vuex-store/index'
78

89
Vue.config.productionTip = false
910

11+
sync(store, router) // done.
12+
1013
/* eslint-disable no-new */
1114
new Vue({
1215
el: '#app',

lesson/04.vue-vuex-todos/src/vuex-store/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import uuid from 'uuid'
55
Vue.use(Vuex)
66

77
export default new Vuex.Store({
8+
// 在严格模式下,无论何时发生了状态变更且不是由 mutation 函数引起的,将会抛出错误。
9+
// 这能保证所有的状态变更都能被调试工具跟踪到。
10+
strict: process.env.NODE_ENV !== 'production',
811
state: {
912
todos: [] // 存储所有todos
1013
},
@@ -38,9 +41,6 @@ export default new Vuex.Store({
3841

3942
},
4043
getters: {
41-
All: state => state.todos,
42-
Active: state => state.todos.filter(todo => !todo.completed),
43-
Completed: state => state.todos.filter(todo => todo.completed),
4444
total: state => state.todos.length,
4545
remaining: state => state.todos ? state.todos.filter(todo => !todo.completed).length : 0
4646
}

0 commit comments

Comments
 (0)