Skip to content

Commit 37fbc2f

Browse files
author
biao.chen
committed
feat: 用组合式api重构评论功能
1 parent beea44a commit 37fbc2f

File tree

4 files changed

+119
-114
lines changed

4 files changed

+119
-114
lines changed

src/components/ArrowUp.vue

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,39 @@
99
</div>
1010
</template>
1111
<script lang="ts">
12-
import { defineComponent } from "vue";
12+
import { defineComponent, reactive, onMounted } from "vue";
1313
1414
export default defineComponent({
1515
name: "ArrowUp",
16-
data() {
17-
return {
16+
setup() {
17+
const state = reactive({
1818
isShowBtn: false,
19-
};
20-
},
21-
mounted() {
22-
// 当网页向下滑动 20px 出现"返回顶部" 按钮
23-
window.onscroll = (): void => {
24-
if (
25-
window.document.body.scrollTop > 100 ||
26-
window.document.documentElement.scrollTop > 100
27-
) {
28-
this.isShowBtn = true;
29-
} else {
30-
this.isShowBtn = false;
31-
}
32-
};
33-
},
34-
methods: {
35-
topFunction(): void {
19+
});
20+
21+
onMounted(() => {
22+
// 当网页向下滑动 20px 出现"返回顶部" 按钮
23+
window.onscroll = (): void => {
24+
if (
25+
window.document.body.scrollTop > 100 ||
26+
window.document.documentElement.scrollTop > 100
27+
) {
28+
state.isShowBtn = true;
29+
} else {
30+
state.isShowBtn = false;
31+
}
32+
};
33+
});
34+
35+
const topFunction = (): void => {
3636
document.body.scrollTop = 0;
3737
document.documentElement.scrollTop = 0;
38-
},
38+
}
39+
40+
return {
41+
state,
42+
topFunction
43+
};
3944
},
40-
setup() {},
4145
});
4246
</script>
4347
<style scoped>

src/components/Comment.vue

Lines changed: 48 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
<el-dialog
33
title="评论"
44
width="60%"
5-
v-model="dialogDodel"
5+
v-model="state.dialogDodel"
66
@close="cancel"
77
>
88
<el-form>
99
<el-form-item>
1010
<el-input
1111
type="textarea"
12-
v-model="content"
12+
v-model="state.content"
1313
placeholder="文明社会,理性评论"
1414
autocomplete="off"
1515
></el-input>
@@ -32,9 +32,10 @@
3232
</template>
3333

3434
<script lang="ts">
35-
import config from "../utils/config";
36-
import { ToUser } from "../types/index";
37-
import { defineComponent } from "vue";
35+
import { ElMessage } from "element-plus";
36+
import { defineComponent, reactive, watch } from "vue";
37+
import service from "../utils/https";
38+
import urls from "../utils/urls";
3839
3940
export default defineComponent({
4041
name: "Comment",
@@ -57,53 +58,31 @@ export default defineComponent({
5758
},
5859
},
5960
emits: ["ok", "cancel"],
60-
data() {
61-
return {
62-
dialogDodel: this.visible,
61+
setup(props, context) {
62+
const state = reactive({
63+
dialogDodel: props.visible,
6364
btnLoading: false,
6465
content: "",
6566
cacheTime: 0, // 缓存时间
6667
times: 0, // 留言次数
67-
};
68-
},
69-
watch: {
70-
dialogDodel: {
71-
handler(val: any, oldVal: any) {
72-
if (!val) {
73-
this.$emit("cancel", val);
74-
}
75-
},
76-
// immediate: true
77-
},
78-
visible: {
79-
handler(val: any, oldVal: any) {
80-
console.log("val: ", val);
81-
this.dialogDodel = val;
82-
},
83-
// immediate: true
84-
},
85-
},
86-
// computed: {
87-
// dialogVisible(): boolean {
88-
// return this.visible;
89-
// },
90-
// },
91-
methods: {
92-
cancel(): boolean {
93-
this.$emit("cancel", false);
68+
});
69+
70+
const cancel = (): boolean => {
71+
context.emit("cancel", false);
9472
return false;
95-
},
96-
async handleOk(): Promise<void> {
97-
if (!this.article_id) {
98-
(this as any).$message({
73+
};
74+
75+
const handleOk = async (): Promise<void> => {
76+
if (!props.article_id) {
77+
ElMessage({
9978
message: "该文章不存在!",
10079
type: "error",
10180
});
10281
return;
10382
}
10483
105-
if (this.times > 2) {
106-
(this as any).$message({
84+
if (state.times > 2) {
85+
ElMessage({
10786
message: "您今天评论的次数已经用完,明天再来评论吧!",
10887
type: "warning",
10988
});
@@ -112,16 +91,16 @@ export default defineComponent({
11291
11392
let now = new Date();
11493
let nowTime = now.getTime();
115-
if (nowTime - this.cacheTime < 4000) {
116-
(this as any).$message({
94+
if (nowTime - state.cacheTime < 4000) {
95+
ElMessage({
11796
message: "您评论太过频繁,1 分钟后再来评论吧!",
11897
type: "warning",
11998
});
12099
return;
121100
}
122101
123-
if (!this.content) {
124-
(this as any).$message({
102+
if (!state.content) {
103+
ElMessage({
125104
message: "评论内容不能为空",
126105
type: "error",
127106
});
@@ -133,33 +112,42 @@ export default defineComponent({
133112
let userInfo = JSON.parse(window.sessionStorage.userInfo);
134113
user_id = userInfo._id;
135114
} else {
136-
(this as any).$message({
115+
ElMessage({
137116
message: "登录才能评论,请先登录!",
138117
type: "warning",
139118
});
140119
return;
141120
}
142-
this.btnLoading = true;
143-
await (this as any).$https.post((this as any).$urls.addThirdComment, {
144-
article_id: this.article_id,
121+
state.btnLoading = true;
122+
await service.post(urls.addThirdComment, {
123+
article_id: props.article_id,
145124
user_id,
146-
comment_id: this.comment_id,
147-
to_user: JSON.stringify(this.to_user),
148-
content: this.content,
125+
comment_id: props.comment_id,
126+
to_user: JSON.stringify(props.to_user),
127+
content: state.content,
149128
});
150-
this.btnLoading = false;
151-
this.times++;
129+
state.btnLoading = false;
130+
state.times++;
152131
153-
this.cacheTime = nowTime;
154-
this.content = "";
155-
this.$emit("ok", false);
156-
(this as any).$message({
132+
state.cacheTime = nowTime;
133+
state.content = "";
134+
context.emit("ok", false);
135+
ElMessage({
157136
message: "操作成功",
158137
type: "success",
159138
});
160-
},
139+
};
140+
141+
watch(props, (val, oldVal) => {
142+
state.dialogDodel = val.visible;
143+
});
144+
145+
return {
146+
state,
147+
cancel,
148+
handleOk
149+
};
161150
},
162-
setup() {},
163151
});
164152
</script>
165153
<style scoped>

src/components/CommentList.vue

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,22 @@
8888
</div>
8989
</div>
9090
<Comment
91-
:visible="visible"
92-
:to_user="to_user"
93-
:comment_id="comment_id"
91+
:visible="state.visible"
92+
:to_user="state.to_user"
93+
:comment_id="state.comment_id"
9494
:article_id="article_id"
9595
@ok="handleOk"
9696
@cancel="handleCancel"
9797
/>
9898
</div>
9999
</template>
100100
<script lang="ts">
101-
import { defineComponent, defineAsyncComponent } from "vue";
101+
import { ElMessage } from "element-plus";
102+
import {
103+
defineComponent,
104+
defineAsyncComponent,
105+
reactive,
106+
} from "vue";
102107
import { timestampToTime } from "../utils/utils";
103108
import { ToUser } from "../types/index";
104109
@@ -121,57 +126,65 @@ export default defineComponent({
121126
default: "",
122127
},
123128
},
124-
data() {
125-
return {
129+
setup(props, context) {
130+
const state = reactive({
126131
visible: false,
127132
comment_id: "",
128133
to_user: {
129134
user_id: "",
130135
name: "",
131136
avatar: "",
132137
type: 0,
133-
} as ToUser,
134-
};
135-
},
136-
methods: {
137-
formatTime(value: string | Date): string {
138+
}
139+
});
140+
141+
const formatTime = (value: string | Date): string => {
138142
return timestampToTime(value, true);
139-
},
140-
handleCancel(): void {
141-
this.visible = false;
142-
},
143-
handleOk(): void {
144-
this.visible = false;
145-
this.$emit("refreshArticle");
146-
},
143+
};
144+
145+
const handleCancel = (): void => {
146+
state.visible = false;
147+
};
148+
149+
const handleOk = (): void => {
150+
state.visible = false;
151+
context.emit("refreshArticle");
152+
};
153+
147154
// 添加评论
148-
showCommentModal(
155+
const showCommentModal = (
149156
commitId: string,
150157
user: ToUser,
151158
secondUser?: ToUser
152-
): boolean | void {
159+
): boolean | void => {
153160
if (!window.sessionStorage.userInfo) {
154-
(this as any).$message({
161+
ElMessage({
155162
message: "登录才能点赞,请先登录!",
156163
type: "warning",
157164
});
158165
return false;
159166
}
160167
// 添加三级评论
161168
if (secondUser) {
162-
this.visible = true;
163-
this.comment_id = commitId;
164-
this.to_user = user;
169+
state.visible = true;
170+
state.comment_id = commitId;
171+
state.to_user = user;
165172
} else {
166173
// 添加二级评论
167-
this.visible = true;
168-
this.comment_id = commitId;
169-
this.to_user = user;
174+
state.visible = true;
175+
state.comment_id = commitId;
176+
state.to_user = user;
170177
}
171-
console.log("this.visible: ", this.visible);
172-
},
178+
};
179+
180+
return {
181+
state,
182+
showCommentModal,
183+
handleOk,
184+
handleCancel,
185+
formatTime
186+
};
173187
},
174-
setup() {},
175188
});
176189
</script>
177190
<style lang="less" scoped>

src/components/RegisterAndLogin.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<el-dialog
33
title="登录"
4-
:width="state.isMobile ? '90%' : '50%'"
4+
:width="isMobile ? '90%' : '50%'"
55
v-model="state.dialogDodel"
66
@close="cancel"
77
:show-close="true"

0 commit comments

Comments
 (0)