const{createApp}=Vue const{createVuetify,useGoTo,useDisplay}=Vuetify var data={alert:{show:false,color:'success',text:'',timeout:0,},theme:{dark:false,},nav:{showDrawer:false,showTOC:true,tocPanel:0,tab:'account',post:{discussionId:7370,currentPage:1,targetPage:1,total:16,anchor:0,goToOptions:{container:null,duration:0,easing:'easeInOutCubic',offset:-100,},worker:null,task:[],active:[],apiLock:[],originLike:new Map([]),},related:{block:1,}},search:{width:80,text:null,loading:false,},tags:[{id:20,url:'/t/2c0b5e546a045c756077023a1a1e',name:'Js|Ts|H5',color:'#FFA726',icon:'mdi-language-javascript',},],posts:[{id:93600,num:0,uid:620,content:'昨天下班前碰到一个问题,我是自己写的 loading 组件,注册了全局指令。在指令引用的时候如果 loading 是对象在更新属性时指令方法 updated 不会捕获到修改,v-loading 用扩展运算或者在 template 里引用则能正确更新状态。能在哪里做些调整吗?\u003Cbr\u003E\u003Cbr\u003E![code]( https://imgur.com/a/1GmgAgO)\u003Cbr\u003E\u003Cbr\u003E![direactive]( https://imgur.com/a/mLmEw9I)\u003Cbr\u003E\u003Cbr\u003E\u003Cbr\u003E\u003Cbr\u003E另外,不确定自己封装通用组件的必要性,现在的是 C 端项目有 ui 要求没用模板,但也有像 form 、table 、dialog 等模块,因为工作量不大我现在都单独抽出了组件库写了文档。越优化越觉得和 element 的组件功能是差不多的只是 ui 不同,引用 element 然后自定义样式,不满足的功能也可以二次封装自己添加,按需加载打包可能更方便?',ipRegion:'',updatedByUid:0,createdAt:'2025-04-01 10:08:16',updatedAt:'2025-04-03 11:14:56',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:93601,num:1,uid:796,content:'let loading \u003D ref(false)',ipRegion:'',updatedByUid:0,createdAt:'2025-04-01 10:21:11',updatedAt:'2025-04-03 11:14:56',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:93602,num:2,uid:813,content:'在 mounted 和 updated 中 watch 一下 bindingValue ,根据这边变量的用法,属性变化不会触发 updated 。用 element-plus 覆盖样式也挺香,或者 fork 下来改改',ipRegion:'',updatedByUid:0,createdAt:'2025-04-01 10:25:37',updatedAt:'2025-04-03 11:14:56',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:93603,num:3,uid:4632,content:'https://github.com/vuejs/core/blob/f6e84af30aeffd281aebbab02b0e623e5fc159e0/packages/runtime-core/src/directives.ts#L95\u003Cbr\u003E\u003Cbr\u003E有一个 deep 的配置,如果加一个 deep:ture 就可以了,因为在 mount 时,默认没有对这个响应式数据的各个 key 进行依赖收集,所以才没有触发对应的 update 钩子,\u003Cbr\u003E\u003Cbr\u003E如果你在模板中使用使用{{loadingValueObj}},是直接编译成_toDisplayString($setup.loadingValueObj),里面是使用的 JSON.stringify ,就对各个属性进行了依赖收集了',ipRegion:'',updatedByUid:0,createdAt:'2025-04-01 10:49:17',updatedAt:'2025-04-03 11:14:56',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:93604,num:4,uid:620,content:'boolean 是正常的,用 object 是因为要设置其他属性,已经按 3#的方法修改了',ipRegion:'',updatedByUid:0,createdAt:'2025-04-01 10:59:18',updatedAt:'2025-04-03 11:14:56',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:93605,num:5,uid:620,content:'原本建项目的时候就在想要不要引 elementplus ,因为以前也一直用这个,原本二次封装的也能直接拿来用。最后还是感觉太重了就自己撸,现在想想可能草率了。XD',ipRegion:'',updatedByUid:0,createdAt:'2025-04-01 11:04:12',updatedAt:'2025-04-03 11:14:56',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:93606,num:6,uid:620,content:'非常感谢!这是不爱读源码的坏处吗 XD',ipRegion:'',updatedByUid:0,createdAt:'2025-04-01 11:09:27',updatedAt:'2025-04-03 11:14:56',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:93607,num:7,uid:9490,content:'ref 也能处理 object 啊,reactive 就是因为使用中容易出现像你这种丢失响应性的问题,其实官方更推荐 ref',ipRegion:'',updatedByUid:0,createdAt:'2025-04-01 11:09:42',updatedAt:'2025-04-03 11:14:56',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:93608,num:8,uid:4632,content:'这个问题的本质是,渲染函数的副作用没有对这种情况进行依赖收集,这就会导致组件始终不会更新了,那么自定义指令的 update 钩子也当然无法执行了',ipRegion:'',updatedByUid:0,createdAt:'2025-04-01 11:18:15',updatedAt:'2025-04-03 11:14:56',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:93609,num:9,uid:620,content:'ref 之前试过了也不会更新, 说的是对的',ipRegion:'',updatedByUid:0,createdAt:'2025-04-01 11:24:05',updatedAt:'2025-04-03 11:14:56',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:93610,num:10,uid:796,content:'ref 和 reactive 的本质区别就是 ref 一定不会出错 reactive 就是会出现意料之外的情况\u003Cbr\u003E只要 ref.value 去重新赋值,一定触发更新。我从来不在项目里写 reactive',ipRegion:'',updatedByUid:0,createdAt:'2025-04-01 12:54:58',updatedAt:'2025-04-03 11:14:56',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:93611,num:11,uid:620,content:'\u003Cbr\u003E\u003Cbr\u003E修正一下我 9#的错误回复,我又试了一下 ref 定义 loading 变量,在对 loading.value 整体重新赋值后确实会触发更新,我的结论是基于对 loading.value.xx 赋值,这是不能捕获修改的,原因就是 说的创建时默认没有对 object key 监听,进行引用和扩展类似创建新对象则会触发 update ,deep 和`watch binding`则是主动激活监听。\u003Cbr\u003E\u003Cbr\u003E```\u003Cbr\u003Econst loading \u003D ref(false)\u003Cbr\u003Eloading.value \u003D { loading: true, text: \u0026#39;loading...\u0026#39; }\u003Cbr\u003E// 状态更新正常\u003Cbr\u003E\u003Cbr\u003Econst loading \u003D ref({ loading: false })\u003Cbr\u003Eloading.value \u003D { loading: true }\u003Cbr\u003E// 状态更新正常\u003Cbr\u003E\u003Cbr\u003Econst loading \u003D ref({ loading: false })\u003Cbr\u003Eloading.value.loading \u003D true\u003Cbr\u003E// 无法更新\u003Cbr\u003E```\u003Cbr\u003E\u003Cbr\u003E不过从组件的角度肯定是不限制 ref 和 reactive 更合适,因为没有严格的代码规范并不能要求所有人都用 ref.value 赋值。',ipRegion:'',updatedByUid:0,createdAt:'2025-04-01 14:27:56',updatedAt:'2025-04-03 11:14:56',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:93612,num:12,uid:796,content:'loading.value.loading \u003D true 按照道理是会触发更新的,我一直所有代码都是这么写的,肯定是哪里写错了引用变了,Vue devtools 你点进去看看就知道了\u003Cbr\u003E喜欢用 reactive 就用,就像有些人喜欢父子传值传进去再 watch 写的复杂的不得了,我是感觉不如 expose 个方法出去让父组件 ref 调用方法一次性传值。这种写法出来的程序只能在中午运行,早晚出问题',ipRegion:'',updatedByUid:0,createdAt:'2025-04-01 14:39:51',updatedAt:'2025-04-03 11:14:56',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:93613,num:13,uid:10002,content:'还 ref 一定不出错,对象用 ref 声明,ref.value 就是个 reactive ,用得不好一样出错',ipRegion:'',updatedByUid:0,createdAt:'2025-04-01 17:24:51',updatedAt:'2025-04-03 11:14:56',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:93614,num:14,uid:12989,content:'watch 默认下不会深入观察 ref ,要加 deep 参数:\u003Cbr\u003E```j**ascript\u003Cbr\u003Ewatch(loading, () \u003D\u0026gt; { \u003Cbr\u003E console.log(\u0026#39;ref deep watch\u0026#39;)\u003Cbr\u003E}, { deep: true })\u003Cbr\u003E```',ipRegion:'',updatedByUid:0,createdAt:'2025-04-01 17:26:54',updatedAt:'2025-04-03 11:14:56',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:93615,num:15,uid:7315,content:'这句话并不对啊,确实没有规范要求用 ref 还是 reactive ,但作为组件,我只需要规定 binding.value 是一个 bool 类型就好了,不管你是基本类型还是对象类型,如果用对象,就应该吧 loading 这单个属性传递给我。参考 element ui ,你的其他 text ,color ,size 属性,应该通过 el 的 dataset 获取,不应该在 binding.value 中。',ipRegion:'',updatedByUid:0,createdAt:'2025-04-01 18:23:28',updatedAt:'2025-04-03 11:14:56',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},],usersMap:new Map([[4632,{uid:4632,url:'/u/0e784a596a045c736669546f1f222435',avatar:'/a/0e784a596a045c736669546f1f222435',username:'ooo4🤖',}],[9490,{uid:9490,url:'/u/313d733f6a045c7e6463566f3825670b',avatar:'/a/313d733f6a045c7e6463566f3825670b',username:'NerbraskaGuy🤖',}],[10002,{uid:10002,url:'/u/08286b5a6a045d77606a546f2f071d29',avatar:'/a/08286b5a6a045d77606a546f2f071d29',username:'sakura1988🤖',}],[12989,{uid:12989,url:'/u/397b575c6a045d7569625f6f315f1e09',avatar:'/a/397b575c6a045d7569625f6f315f1e09',username:'Tokin🤖',}],[813,{uid:813,url:'/u/0f1c7f1d6a045c77686b556f25001c22',avatar:'/a/0f1c7f1d6a045c77686b556f25001c22',username:'ljl024🤖',}],[796,{uid:796,url:'/u/0f0c05056a045c776763506f0e282712',avatar:'/a/0f0c05056a045c776763506f0e282712',username:'wangtian2020🤖',}],[7315,{uid:7315,url:'/u/32245f586a045c70636b536f15062224',avatar:'/a/32245f586a045c70636b536f15062224',username:'SanjinGG🤖',}],[620,{uid:620,url:'/u/0f7048546a045c776668566f253c360d',avatar:'/a/0f7048546a045c776668566f253c360d',username:'le**eeel🤖',}],]),related:[{title:'推荐一款自己写的提升效率的浏览器插件: HarmonyAutoCopy,让文本复制更轻松!',url:'/d/6f06033d6a045c77606a567a475c646a1f371708',},{title:'程序猿创业失败,转行做独立开发',url:'/d/393e7d3e6a045c77606a56754350616a59191c16',},{title:'发现一个有意思的项目,把一个字符串隐藏到另一个字符串中',url:'/d/3c117a346a045c77606a56754359676a5b323b0b',},{title:'编程小白,终于成功上线了自己的第一个导航站😭 激动',url:'/d/333944256a045c77606a56754451666a3e0e3e67',},{title:'小红书 | 图文,视频,评论 浏览与导出工具',url:'/d/1b1806556a045c77606a56754458676a04343b26',},{title:'[BadouCMS] Vue3+TypeScript+ThinkPHP8 构建 CMS 系统(开源)',url:'/d/303350036a045c77606a56754458626a20390564',},{title:'开发了一个开箱即用的本地终端 Ztty',url:'/d/0c045a2e6a045c77606a5675455e6b6a3e200734',},{title:'第一个完全用 AI 工具生成的工具站',url:'/d/6a18771a6a045c77606a5675475e616a01126104',},{title:'为什么 vue 的 nuxt.js 不跟进 nextjs 的 app route 目录结构',url:'/d/02116a1e6a045c77606a56744f516a6a1b02141e',},{title:'Vite 开发服务器路径遍历漏洞(CVE-2025-30208)',url:'/d/2f2f0a2b6a045c77606a56744f5a646a231a021b',},{title:'做定时任务,一定要用这个神库!',url:'/d/2a1a550b6a045c77606a56744f5b626a5c151613',},{title:'XUGOU - 轻量级系统监控平台,基于 CloudFlare 零成本部署!',url:'/d/0a21430f6a045c77606a56744f58656a0f741e25',},{title:'NextJS 超瓜皮漏洞,赶紧升级!',url:'/d/03226a096a045c77606a56744258646a59126264',},{title:'Typescript 如此成功,为何没有发展出所谓 “Typthon”?',url:'/d/35315c016a045c77606a5674445f626a0e730224',},{title:'为什么 Python 、Node.js 就不能学习一下 C#这种优雅的依赖管理方式?',url:'/d/0f1f5f3d6a045c77606a5674445c676a04311f60',},{title:'求助!双显卡连接 6 个屏幕,展示不同的 URL 页面。Electron 的 displayId 每次重启都会变',url:'/d/1f3158176a045c77606a56744551676a24111b3b',},{title:'关于 react native 和 flutter',url:'/d/6f7c03286a045c77606a56774e50616a27221935',},{title:'[野生程序员花三天时间用 Cursor 复刻经典游戏「俄罗丝方块」,求各位提点建议]',url:'/d/6b047a5e6a045c77606a56774e5c626a2b361d65',},{title:'分享一个用 JS、canvas 写的星空穿越效果',url:'/d/191c61346a045c77606a5677425e6a6a2f28273c',},{title:'郑州招 Python , Node.js 开发岗',url:'/d/082c61146a045c77606a5677425e616a3d706116',},],} const App={setup(){const goTo=useGoTo() const{mdAndUp}=useDisplay() return{goTo,mdAndUp}},data(){return data;},mounted(){const themeDark=localStorage.getItem("themeDark") if(themeDark!==null){this.theme.dark=JSON.parse(themeDark)} if(this.nav.post.total>(this.nav.post.currentPage-1)*100+20){let moreLen=100 if(this.nav.post.total({id:null,num:(this.nav.post.currentPage-1)*100+v,uid:null,content:null,ipRegion:null,updatedByUid:null,createdAt:null,updatedAt:null,mentionNum:null,mentionedBy:null,mentionUsers:null,likeUsers:null,})) this.posts.push(...morePosts.slice(20))} this.workerStart() const hash=window.location.hash const match=hash.match(/#(\d+)/) if(match){const n=parseInt(match[1],10) if(n>=(this.nav.post.currentPage-1)*100&&n{this.jumpTo(n)})}} this.$nextTick(()=>{this.addHeadingIds() tocbot.init({tocSelector:'.toc',contentSelector:'#post-content-0',headingSelector:'h2, h3, h4',headingsOffset:100,scrollSmoothOffset:-100,scrollSmooth:true,collapseDepth:6,onClick:function(e){setTimeout(()=>{history.replaceState(null,'',window.location.pathname+window.location.search)},0)},}) tocbot.refresh()});},beforeUnmount(){this.workerStop() if(this.quill){this.quill.destroy() this.quill=null}},computed:{dposts(){return this.posts.slice(20);},},created(){},methods:{successAlert(msg){this.alert={show:true,color:'success',text:msg,timeout:1500,}},failureAlert(msg){this.alert={show:true,color:'error',text:msg,timeout:5000,}},flipThemeDark(){this.theme.dark=!this.theme.dark localStorage.setItem("themeDark",JSON.stringify(this.theme.dark))},toSearch(){if(!this.search.text){this.failureAlert('搜索词不能为空') return} let keywords=this.search.text.trim() if(keywords.length<1){this.failureAlert('搜索词不能为空') return} if(keywords.length>100){this.failureAlert('搜索词过长') return} this.doSearch(keywords)},toReg(){window.location.href="/reg"},toLogin(){window.location.href="/login"},toPage(){let url=window.location.href url=url.replace(/(\/\d+)?(#[0-9]+)?$/,this.nav.post.targetPage>1?`/${this.nav.post.targetPage}`:'') window.location.href=url},toLoadRelated({done}){if(this.my&&this.my.uid){this.apiLoadRelated({done})}else{done('ok')}},workerStart(){this.nav.post.worker=setInterval(()=>{this.workerLoad()},500);},workerStop(){if(this.nav.post.worker){clearInterval(this.nav.post.worker);this.nav.post.worker=null;}},async jumpTo(num){const page=Math.floor(num/100)+1 const i=num-(page-1)*100 if(page===this.nav.post.currentPage){this.goTo("#post-"+num,this.nav.post.goToOptions) if(!this.posts[i].id){const block=Math.floor(num/20)+1 this.nav.post.apiLock[block]=true await this.apiLoadPosts(block) this.$nextTick(()=>{this.goTo("#post-"+num,this.nav.post.goToOptions)})}}else{let url=window.location.href url=url.replace(/(\/\d+)?(#[0-9]+)?$/,page>1?`/${page}`:'') url=url+"#"+num window.location.href=url}},postIntersect(num){return(isIntersecting,entries,observer)=>{if(isIntersecting){this.nav.post.task.push(num) this.nav.post.active.push(num) this.nav.post.active=this.nav.post.active.filter(item=>Math.abs(item-num)<=5) this.nav.post.active.sort((a,b)=>a-b)}else{this.nav.post.active=this.nav.post.active.filter(item=>item!==num)} if(this.nav.post.active[0]){this.nav.post.anchor=this.nav.post.active[0]}else{this.nav.post.anchor=0}}},async apiLoadPosts(block){try{const response=await axios.post('/fapi/v1/post/block/'+block,{discussionId:this.nav.post.discussionId,}) if(response.data.code===0){response.data.data.posts.forEach(post=>{const i=post.num%100 Object.assign(this.posts[i],post)}) response.data.data.users.forEach(user=>{this.usersMap.set(user.uid,user)})}else{this.failureAlert('回帖数据加载失败: '+response.data.msg)}}catch(error){this.failureAlert('回帖数据加载失败: '+error)} this.nav.post.apiLock[block]=false},workerLoad(){while(this.nav.post.task.length){const num=this.nav.post.task.pop() const i=num-(this.nav.post.currentPage-1)*100 if(!this.posts[i].id){const block=Math.floor(num/20)+1 if(!this.nav.post.apiLock[block]){this.nav.post.apiLock[block]=true this.apiLoadPosts(block)}}}},getTimeInfo(t){if(!t){return ""} const now=new Date();const then=new Date(t);const diff=now-then;const minute=60*1000;const hour=minute*60;const day=hour*24;const month=day*30;const year=month*12;if(diffpost.num===num) if(!post){return "#"+num} const uid=post.uid const username=this.usersMap.get(uid)?.username if(!username){return "#"+num} return username},getUsernameByPostId(id){const post=this.posts.find(post=>post.id===id) if(!post){return "#"+this.getPostNumByPostId(id)} const uid=post.uid const username=this.usersMap.get(uid).username if(!username){return "#"+this.getPostNumByPostId(id)} return username},getPostNumByPostId(id){const post=this.posts.find(post=>post.id===id) return post.num},getPostById(id){const post=this.posts.find(post=>post.id===id) return post},getPostByNum(num){const post=this.posts.find(post=>post.num===num) return post},getAvatarByUid(uid){const avatar=this.usersMap.get(uid)?.avatar if(!avatar){return this.getRandomAvatar()} return avatar},getAvatarByPostNum(num){const post=this.posts.find(post=>post.num===num) if(!post){return this.getRandomAvatar()} const uid=post.uid return this.getAvatarByUid(uid)},getRandomAvatar(){const num=Math.floor(Math.random()*100) return "https://randomuser.me/api/portraits/men/"+num+".jpg"},getUrlByUid(uid){const url=this.usersMap.get(uid)?.url if(!url){return ""} return url},getTextByPostNum(num){const post=this.posts.find(post=>post.num===num) if(!post||!post.content){return '点击跳转到#'+num+'查看'} const parser=new DOMParser() const doc=parser.parseFromString(post.content,'text/html') const text=doc.body.textContent||'' return text.slice(0,100)},addHeadingIds(){const content=document.getElementById('post-content-0') if(!content){this.nav.showTOC=false return} const headings=content.querySelectorAll('h2, h3, h4') headings.forEach((heading,index)=>{if(!heading.id){heading.id=`toc-nav-${index}`}}) if(headings.length==0){this.nav.showTOC=false}},async doSearch(keywords){this.search.loading=true try{const response=await axios.post('/fapi/v1/search',{keywords:keywords,}) if(response.data.code===0){if(response.data.data.hash&&response.data.data.hash.length===32){window.location.href="/s/"+response.data.data.hash}else{this.failureAlert('搜索失败: 搜索服务异常')}}else{this.failureAlert('搜索失败: '+response.data.msg)}}catch(error){this.failureAlert('搜索失败: '+error)} this.search.loading=false},debounce(fn,delay){let timer=null return function(...args){if(timer)clearTimeout(timer) timer=setTimeout(()=>{fn.apply(this,args)},delay);};},},watch:{'nav.post.targetPage':{handler:async function(newV,oldV){this.toPage()},immediate:false},},} const vuetify=createVuetify({defaults:{global:{ripple:true,},},}) const app=createApp(App) app.use(vuetify).mount("#app")