我的Vue之旅 10 Gin重写后端、实现页面详情页 Mysql + Golang + Gin( 四 )

views/CommentView.vue修改组件,添加DEBUGFLAG选项,添加传入初始pid 。
<template><div v-if="debug" class="m-2"><div class="text-3xl font-bold">[DEBUG] Query Comments</div> ......</div><div v-if="debug" class="m-2"><div class="text-3xl font-bold">[DEBUG] Insert Comments</div>......</div><div class="m-4 border-stone-500"><textareaid="text"class="input_text h-20 w-full"rows="3"cols="40"placeholder="Write your comment..."v-model="text"/></div><inputtype="button"value="https://www.huyubaike.com/biancheng/Post comment"class="w-32h-10text-centerml-4text-sm text-whiteinline-blockfont-boldrounded-md"style="background-color: #fa5c5c"@click="insertComment"/><comment-area:comments="comments":uid="uid"@delete-comment="deleteComment"></comment-area></template><script>import CommentArea from '@/components/common/CommentArea.vue';export default {components: { CommentArea },name: 'CommentTestView',props: ["debug", "initpid"],data: function () {return {pid: 0,uid: 1003, // TODO VUEX 保存用户UUIDtext: "",comments: []}},methods: {insertComment() {const params = new URLSearchParams();params.append('uid', this.uid)params.append('pid', this.pid)params.append('text', this.text)this.axios.post("comment/insert",params).then(response => {console.log(response.data)this.comments.unshift(response.data)console.log(this.comments)}).catch(err => {console.log(err)})},deleteComment(id) {const params = new URLSearchParams();params.append('id', id)this.axios.post("comment/delete", params).then(response => {console.log(response.data)this.comments = this.comments.filter(elem => {return elem.id != id})}).catch(err => {console.log(err)})},queryComment() {this.axios.get("comment/query", {params: {pid: this.pid}}).then(response => {if (!response.data) {this.comments = []return}this.comments = response.datathis.comments.reverse()}).catch(err => {console.log(err)})}},created() {this.pid = this.initpid;this.queryComment();let old = localStorage.getItem(`comment_${this.pid}`)if (old) {this.text = old}},watch: {text() {localStorage.setItem(`comment_${this.pid}`, this.text)}}}</script><style scoped>.input_text {@apply mt-2inline-blockbg-whitefocus:outline-none focus:ring focus:border-blue-200py-1.5pl-3border border-stone-400text-sm;}.input_button {@apply border border-rose-400text-smfont-boldtext-rose-500rounded-smpx-4py-1mt-2ml-4active:bg-rose-400 active:text-white;}</style>资料参考Minimal Example | Axios Docs (axios-http.com)
JSON.parse() - JavaScript | MDN (mozilla.org)
vue-router/" rel="external nofollow noreferrer">Dynamic routing using Vue Router - LogRocket Blog
常见的HTTP状态码及HTTP状态码大全-太平洋IT百科 (pconline.com.cn)
go-sql-driver/mysql: Go MySQL Driver is a MySQL driver for Go's (golang) database/sql package (github.com)
gin package - github.com/gin-gonic/gin - Go Packages
embedresponsively.com
【我的Vue之旅 10 Gin重写后端、实现页面详情页 Mysql + Golang + Gin】

经验总结扩展阅读