网站地址免费,广州网络营销产品代理,张雪峰谈广告类专业,百科类网站建设1、 功能效果 在线预览#xff1a;https://szhihao.gitee.io/comment/
gitee仓库地址#xff1a;https://gitee.com/szhihao/comment
2、实现的具体技术点 根据不同的人名可以进行评论#xff08;tap切换#xff09; 对进行的评论可以无限进行回复#xff08;递归组件和…1、 功能效果 在线预览https://szhihao.gitee.io/comment/
gitee仓库地址https://gitee.com/szhihao/comment
2、实现的具体技术点 根据不同的人名可以进行评论tap切换 对进行的评论可以无限进行回复递归组件和数据tree处理 判断评论是否为空 localStorage 本地存储的使用 vuex 的使用 使用的插件 {name: vite-project,private: true,version: 0.0.0,type: module,scripts: {dev: vite,build: vite build,preview: vite preview},dependencies: {delfin: ^1.2.5,vue: ^3.3.4},devDependencies: {types/node: ^20.5.0,vitejs/plugin-vue: ^4.2.3,vite: ^4.4.5}
}vite.config.js
import { defineConfig } from vite
import vue from vitejs/plugin-vue// 使用path 需要安装 npm install types/node --save-dev
import path from path;//这个path用到了上面安装的types/node
// https://vitejs.dev/config/
export default defineConfig({plugins: [vue()],resolve:{alias:{: path.resolve(./src) // 代替src},extensions:[.js,.vue] // 可以省略.js 和.vue 后缀名}
})app.vue文件
1、定义tab结构作为评论的人名切换
templatedivh1{{ useUserInfoStore.userInfo.userName }}/h1/divbutton:class{bg:item.id useUserInfoStore.userInfo.id }v-for(item,index) in tapList:keyitem.idclickuseUserInfoStore.setUserInfo(item){{item.userName}}/buttonhrcomment-one/comment-one
/template
script setup
import useUserInfo from ./stort/index.js
import {reactive,ref} from vue;
const tabindex ref(-1) //tab的默认值 -1 什么也不显示
import commentOne from ./components/commentOne.vue
const useUserInfoStore useUserInfo()
// 定义人名字数据处理
const tapList reactive([{id:1,userName:孙志豪},{id:2,userName: 王五,},{id:3,userName: 小明,}
])/script
style scoped
.bg {background-color: skyblue;
}
/style2、components/commentOne.vue
当前组件是显示评论的组件里面是 formatTree 数据处理的操作
templatedivptextarea v-modeltextValue/textarea/ppbutton clickaddComment提交评论/button/p/divdivcomment-comp :commentDatadata.commentData add-replyaddReply removeremoveHandler/comment-comp/div
/templatescript setup
import {ref,reactive} from vue;
import useUserInfo from /stort/index.js
// 使用vuex
const useUserInfoStore useUserInfo()
const textValue ref()
import commentComp from ./commentComp.vue
let data reactive({commentData:formatTree(getComment(one)) // 数据tree处理
})const addComment () {//判空处理if(textValue.value undefined || textValue.value null || textValue.value.trim() ){alert(请输入评论)return}// 读取vuex中的数据const {id, userName} useUserInfoStore.userInfolet commentInfo {id: new Date().getTime(),pid: 0,uid: id,userName,comment: textValue.value}setComment(one, commentInfo)// 第一次追加数据解决本地存上数据刷新才会显示data.commentData.unshift(commentInfo)textValue.value
}const addReply (item, replyText) {if(replyText undefined || replyText null || replyText.trim() ) {alert(请输入评论)return}const {id, userName} useUserInfoStore.userInfo;const replyInfo {id: new Date().getTime(),pid: item.id,uid: id,userName,comment: replyText,recoverName:item.userName}// 有子级就将当前数据放进去;(item.children || (item.children [])).unshift(replyInfo)setComment(one, replyInfo);
}// 本地数据的存储
// 1、将本地的数据先取出
// 2、追加新的数据
// 3、将数据转换字符串存入本地function setComment(field, comment) {const commentList JSON.parse(localStorage.getItem(field) || [])commentList.unshift(comment)localStorage.setItem(field, JSON.stringify(commentList))
}// 本地取出数据
function getComment(field) {let arr JSON.parse(localStorage.getItem(field) || [])return arr.reverse()
}// 处理数据处理成tree的结构
function formatTree(data) {const result []const map {}// 以id为属性名作为每一项data.forEach(item {map[item.id] item})data.forEach(item {// 再次循环data数据通过pid可以读取到map中父级的数据,因为子级的pid 是父级的 id 给的子级的pid 父级的 idconst parent map[item.pid]if(parent) {//如果是父级其它数据就是子级// 如果是父级的话并且有 children字段将当前的子级的数据放到children里面没有的话开一个新数组;(parent.children || (parent .children [])).unshift(item)}else {// 循环完了当找找没有子级的数据就将数据放到 result 结果中最后返回result.unshift(item)}})return result
}
/scriptstyle scoped/style
3、components/commentComp.vue
该组件主要作用是递归组件无限评论的核心回复功能的实现
templateliv-for(item,index) in commentData:keyitem.idp {{index 1}}楼 pid:{{item.pid}} --- {{handlerText(item)}} : {{ item.comment }} a hrefjavascript:; clicksetRepFlag(item)回复/a/pdiv v-ifitem.flagptextarea v-modelitem.replyText/textarea/ppbutton clickaddReply(item)提交评论/button/p/divdiv v-ifitem.childrenul!-- 在组件中自己使用自己--comment-comp v-ifitem.children :commentDataitem.children add-replyaddReply /comment-comp/ul/div/li/templatescript setup
const props defineProps({commentData: Array
})// 子穿父 vue3 新语法 Emits
const emit defineEmits([addReply,remove])const setRepFlag (item) {item.flag !item.flag
}
// 提交评论
const addReply (item) {let replyText item.replyTextemit(addReply,item,replyText)item.flag falseitem.replyText
}
// 回复人处理
function handlerText(item) {let recoverName item.recoverName ? item.recoverName : return item.recoverName item.userName ? 我说 : item.userName recoverName 说
}/scriptstyle scoped
li{width: 100%;height: 100%;background-color: skyblue;
}
/style4、vite-project/src/stort/index.js
import {createStore} from delfin
// delfin 插件使用vuex
export default createStore({state:{userInfo:{id:1,userName:孙志豪}},actions:{setUserInfo(store,userInfo) {store.userInfo userInfo}}
})
文章转载自: http://www.morning.ndcjq.cn.gov.cn.ndcjq.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.zwndt.cn.gov.cn.zwndt.cn http://www.morning.mbmh.cn.gov.cn.mbmh.cn http://www.morning.qdsmile.cn.gov.cn.qdsmile.cn http://www.morning.nfccq.cn.gov.cn.nfccq.cn http://www.morning.mmhaoma.com.gov.cn.mmhaoma.com http://www.morning.fynkt.cn.gov.cn.fynkt.cn http://www.morning.pzcjq.cn.gov.cn.pzcjq.cn http://www.morning.rtsx.cn.gov.cn.rtsx.cn http://www.morning.ktblf.cn.gov.cn.ktblf.cn http://www.morning.qjlkp.cn.gov.cn.qjlkp.cn http://www.morning.fnywn.cn.gov.cn.fnywn.cn http://www.morning.wnhsw.cn.gov.cn.wnhsw.cn http://www.morning.rbnp.cn.gov.cn.rbnp.cn http://www.morning.yqkmd.cn.gov.cn.yqkmd.cn http://www.morning.lcplz.cn.gov.cn.lcplz.cn http://www.morning.ntgrn.cn.gov.cn.ntgrn.cn http://www.morning.tqfnf.cn.gov.cn.tqfnf.cn http://www.morning.ctqbc.cn.gov.cn.ctqbc.cn http://www.morning.lchtb.cn.gov.cn.lchtb.cn http://www.morning.bauul.com.gov.cn.bauul.com http://www.morning.pnmtk.cn.gov.cn.pnmtk.cn http://www.morning.mlycx.cn.gov.cn.mlycx.cn http://www.morning.pmdnx.cn.gov.cn.pmdnx.cn http://www.morning.xsjfk.cn.gov.cn.xsjfk.cn http://www.morning.gcfrt.cn.gov.cn.gcfrt.cn http://www.morning.smkxm.cn.gov.cn.smkxm.cn http://www.morning.ssfq.cn.gov.cn.ssfq.cn http://www.morning.xqbgm.cn.gov.cn.xqbgm.cn http://www.morning.zdgp.cn.gov.cn.zdgp.cn http://www.morning.bzcjx.cn.gov.cn.bzcjx.cn http://www.morning.bpzw.cn.gov.cn.bpzw.cn http://www.morning.zbgqt.cn.gov.cn.zbgqt.cn http://www.morning.mmhyx.cn.gov.cn.mmhyx.cn http://www.morning.djbhz.cn.gov.cn.djbhz.cn http://www.morning.tkfnp.cn.gov.cn.tkfnp.cn http://www.morning.bpmdx.cn.gov.cn.bpmdx.cn http://www.morning.dtrzw.cn.gov.cn.dtrzw.cn http://www.morning.qzsmz.cn.gov.cn.qzsmz.cn http://www.morning.gmrxh.cn.gov.cn.gmrxh.cn http://www.morning.jkcnq.cn.gov.cn.jkcnq.cn http://www.morning.rmxk.cn.gov.cn.rmxk.cn http://www.morning.bcnsl.cn.gov.cn.bcnsl.cn http://www.morning.hwlmy.cn.gov.cn.hwlmy.cn http://www.morning.gnbfj.cn.gov.cn.gnbfj.cn http://www.morning.cqyhdy.cn.gov.cn.cqyhdy.cn http://www.morning.phnbd.cn.gov.cn.phnbd.cn http://www.morning.cjnfb.cn.gov.cn.cjnfb.cn http://www.morning.mdwtm.cn.gov.cn.mdwtm.cn http://www.morning.rblqk.cn.gov.cn.rblqk.cn http://www.morning.gbkkt.cn.gov.cn.gbkkt.cn http://www.morning.wtcbl.cn.gov.cn.wtcbl.cn http://www.morning.dtmjn.cn.gov.cn.dtmjn.cn http://www.morning.yqrgq.cn.gov.cn.yqrgq.cn http://www.morning.qbzdj.cn.gov.cn.qbzdj.cn http://www.morning.tqsgt.cn.gov.cn.tqsgt.cn http://www.morning.hnk25076he.cn.gov.cn.hnk25076he.cn http://www.morning.rrhfy.cn.gov.cn.rrhfy.cn http://www.morning.cybch.cn.gov.cn.cybch.cn http://www.morning.mfsjn.cn.gov.cn.mfsjn.cn http://www.morning.ykrss.cn.gov.cn.ykrss.cn http://www.morning.bxfy.cn.gov.cn.bxfy.cn http://www.morning.yhdqq.cn.gov.cn.yhdqq.cn http://www.morning.ylkkh.cn.gov.cn.ylkkh.cn http://www.morning.wqngt.cn.gov.cn.wqngt.cn http://www.morning.qbrdg.cn.gov.cn.qbrdg.cn http://www.morning.fnbtn.cn.gov.cn.fnbtn.cn http://www.morning.pbksb.cn.gov.cn.pbksb.cn http://www.morning.zxcny.cn.gov.cn.zxcny.cn http://www.morning.bmts.cn.gov.cn.bmts.cn http://www.morning.xlpdm.cn.gov.cn.xlpdm.cn http://www.morning.tzkrh.cn.gov.cn.tzkrh.cn http://www.morning.sxlrg.cn.gov.cn.sxlrg.cn http://www.morning.bnwlh.cn.gov.cn.bnwlh.cn http://www.morning.frtb.cn.gov.cn.frtb.cn http://www.morning.jfbrt.cn.gov.cn.jfbrt.cn http://www.morning.rkgyx.cn.gov.cn.rkgyx.cn http://www.morning.kqzxk.cn.gov.cn.kqzxk.cn http://www.morning.rbtny.cn.gov.cn.rbtny.cn