Skip to content

Commit 8dd3cc5

Browse files
author
Alex Yang
committed
Lesson 3 - TodoList
1 parent 6321157 commit 8dd3cc5

File tree

8 files changed

+3090
-188
lines changed

8 files changed

+3090
-188
lines changed

package-lock.json

+2,964-171
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
"webpack-cli": "^3.0.8"
1010
},
1111
"dependencies": {
12+
"boostrap": "^2.0.0",
13+
"bootstrap-vue": "^2.0.0-rc.11",
14+
"css-loader": "^0.28.11",
15+
"moment": "^2.22.2",
16+
"style-loader": "^0.21.0",
1217
"vue": "^2.5.16"
1318
}
1419
}

src/App.vue

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<template>
22
<div id="app">
3-
<HelloVue/>
3+
<TodoList/>
44
</div>
55
</template>
66

77
<script>
8-
import HelloVue from './components/HelloVue.vue'
8+
import TodoList from './components/TodoList.vue'
99
1010
export default {
1111
name: 'app',
1212
components: {
13-
HelloVue
13+
TodoList
1414
}
1515
}
1616
</script>

src/components/HelloVue.vue

-14
This file was deleted.

src/components/TodoList.vue

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<template>
2+
<div id="todolist" class="container" style="margin:20px">
3+
<div class="input-group mb-3">
4+
<div class="input-group-prepend">
5+
<button class="btn btn-outline-secondary" type="button" v-on:click="addTask">添加</button>
6+
</div>
7+
<input type="text" id="task_content" class="form-control" placeholder="请输入任务内容" aria-label="" aria-describedby="basic-addon1">
8+
</div>
9+
<table class="table m-3">
10+
<thead>
11+
<th>任务</th>
12+
</thead>
13+
<tbody>
14+
<tr v-for="task in parsedTaskList">
15+
<td>
16+
<span v-if="isDone(task)" style="color:gray;text-decoration:line-through;">{{ task }}</span>
17+
<span v-else >{{ task }}</span>
18+
</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
</div>
23+
</template>
24+
25+
<script>
26+
import Store from './store.js'
27+
import * as moment from 'moment';
28+
29+
const regex = /[0-9]+:[0-9]+/;
30+
/**
31+
* 该函数作用是解析出字符串中的时间,并将其跟当前时间比较,
32+
* 计算出还剩多久才会到达计划时间,将剩余时间拼接在字符串后。
33+
* 如果当前时间已经过了计划时间,则不对字符串做任何改变
34+
* 例子:
35+
* 23:40 回家 -> 23:40 回家 (还有 6 小时 36 分 15 秒)
36+
*/
37+
const addRemainTime = (task) => {
38+
let result = task.match(regex);
39+
if (result != null && result.length > 0) {
40+
let taskTime = result[0];
41+
let thisMoment = moment();
42+
let currentDate = thisMoment.format('YYYY-MM-DD');
43+
let taskMoment = moment(currentDate + " " + taskTime, 'YYYY-MM-DD HH:mm');
44+
if (taskMoment.valueOf() < thisMoment.valueOf()) {
45+
return task;
46+
}
47+
let duration = moment.duration(taskMoment.diff(thisMoment));
48+
let durationText = duration.hours() + " 小时 " + duration.minutes() + "" + duration.seconds() + "";
49+
return task + " (还有 " + durationText + ")";
50+
}
51+
return task;
52+
}
53+
54+
export default {
55+
name: 'TodoList',
56+
data: function() {
57+
return {
58+
taskList: Store.fetch()
59+
}
60+
},
61+
watch: {
62+
taskList: {
63+
handler: function(tasks) {
64+
Store.save(tasks)
65+
}
66+
}
67+
},
68+
computed: {
69+
parsedTaskList: function () {
70+
let parsedTaskList = [];
71+
for (let i=0; i<this.taskList.length; i++) {
72+
parsedTaskList.push(addRemainTime(this.taskList[i]));
73+
}
74+
return parsedTaskList;
75+
}
76+
},
77+
methods: {
78+
addTask: function(event) {
79+
// 获取任务内容
80+
let task_content = document.querySelector("#task_content");
81+
// 添加任务内容到任务列表中
82+
this.taskList.push(task_content.value);
83+
// 清空任务内容输入框
84+
task_content.value = '';
85+
},
86+
isDone (task) {
87+
let result = task.match(/还有\s[0-9]+\s小时\s[0-9]+\s\s[0-9]+\s/);
88+
return result == null || result.length == 0;
89+
}
90+
}
91+
}
92+
</script>

src/components/store.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const STORAGE_KEY='todo_list'
2+
export default{
3+
fetch(){
4+
return JSON.parse(window.localStorage.getItem(STORAGE_KEY)||'[]')
5+
},
6+
save(items){
7+
window.localStorage.setItem(STORAGE_KEY,JSON.stringify(items))
8+
}
9+
}

src/main.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import Vue from 'vue/dist/vue.js'
22
import App from './App.vue'
3+
import BootstrapVue from 'bootstrap-vue'
4+
import 'bootstrap/dist/css/bootstrap.css'
5+
import 'bootstrap-vue/dist/bootstrap-vue.css'
6+
7+
Vue.use(BootstrapVue);
38

49
new Vue({
510
render: h => h(App)

webpack.config.js

+12
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,23 @@ module.exports = {
99
path: path.resolve(__dirname, 'dist'),
1010
filename: 'bundle.js'
1111
},
12+
watch: false,
13+
watchOptions: {
14+
aggregateTimeout: 3000, // 编译的超时时间,单位:毫秒
15+
poll: 30 // 扫描项目的间隔时间,单位:秒
16+
},
1217
module: {
1318
rules: [
1419
{
1520
test: /\.vue$/,
1621
loader: 'vue-loader',
22+
},
23+
{
24+
test: /\.css$/,
25+
use: [
26+
'style-loader',
27+
'css-loader'
28+
]
1729
}
1830
]
1931
},

0 commit comments

Comments
 (0)