",{"type":32,"value":161}," 或 ",{"type":27,"tag":106,"props":163,"children":165},{"className":164},[],[166],{"type":32,"value":167},"\u003Cuser-profile />",{"type":27,"tag":89,"props":169,"children":171},{"id":170},"_3-避免过度使用-vuex-或-pinia",[172],{"type":32,"value":173},"3. 避免过度使用 Vuex 或 Pinia",{"type":27,"tag":28,"props":175,"children":176},{},[177],{"type":32,"value":178},"将 UI 状态(例如:弹窗的可见性)保留在组件本地,而不是在全局状态管理中。",{"type":27,"tag":28,"props":180,"children":181},{},[182,184,190,191,197],{"type":32,"value":183},"示例: 使用 ",{"type":27,"tag":106,"props":185,"children":187},{"className":186},[],[188],{"type":32,"value":189},"ref",{"type":32,"value":161},{"type":27,"tag":106,"props":192,"children":194},{"className":193},[],[195],{"type":32,"value":196},"reactive",{"type":32,"value":198}," 进来表示临时状态。",{"type":27,"tag":89,"props":200,"children":202},{"id":201},"_4-有效利用插槽",[203],{"type":32,"value":204},"4. 有效利用插槽",{"type":27,"tag":28,"props":206,"children":207},{},[208],{"type":32,"value":209},"使用具名插槽可以提高可重用组件的灵活性并可以清晰的表明使用情况。",{"type":27,"tag":28,"props":211,"children":212},{},[213,215,221,223,229],{"type":32,"value":214},"示例:在 ",{"type":27,"tag":106,"props":216,"children":218},{"className":217},[],[219],{"type":32,"value":220},"Card",{"type":32,"value":222}," 组件创建一个带有 ",{"type":27,"tag":106,"props":224,"children":226},{"className":225},[],[227],{"type":32,"value":228},"\u003Cslot name=\"header\" />",{"type":32,"value":230}," 的组件可以用于创建自定义头部。",{"type":27,"tag":89,"props":232,"children":234},{"id":233},"_5-利用-scoped-限制-css-作用区域",[235],{"type":32,"value":236},"5. 利用 scoped 限制 css 作用区域",{"type":27,"tag":28,"props":238,"children":239},{},[240,242,248],{"type":32,"value":241},"在",{"type":27,"tag":106,"props":243,"children":245},{"className":244},[],[246],{"type":32,"value":247},"\u003Cstyle scoped>",{"type":32,"value":249},"标签上添加 scoped 使用作用域样式来防止 css 冲突。",{"type":27,"tag":28,"props":251,"children":252},{},[253],{"type":32,"value":254},"示例:应用于特定组件的样式而不影响应用程序中其他部分的样式。",{"type":27,"tag":89,"props":256,"children":258},{"id":257},"_6-编写可复用组件",[259],{"type":32,"value":260},"6. 编写可复用组件",{"type":27,"tag":28,"props":262,"children":263},{},[264],{"type":32,"value":265},"将 UI 元素分解为可重用的、集中的组件,避免过于通用的设计。",{"type":27,"tag":28,"props":267,"children":268},{},[269],{"type":32,"value":270},"示例:创建一个支持不同样式的 props 的可配置 Button 组件,而不是对按钮进行硬编码。",{"type":27,"tag":89,"props":272,"children":274},{"id":273},"_7-优雅的处理异步代码",[275],{"type":32,"value":276},"7. 优雅的处理异步代码",{"type":27,"tag":28,"props":278,"children":279},{},[280,282,288],{"type":32,"value":281},"使用",{"type":27,"tag":106,"props":283,"children":285},{"className":284},[],[286],{"type":32,"value":287},"async/await",{"type":32,"value":289},"进行 API 调用并使用一个『全局处理器』来处理错误。",{"type":27,"tag":28,"props":291,"children":292},{},[293,295,301],{"type":32,"value":294},"示例: 创建一个",{"type":27,"tag":106,"props":296,"children":298},{"className":297},[],[299],{"type":32,"value":300},"useApi",{"type":32,"value":302},"composable 来封装 API 请求逻辑和错误处理逻辑。",{"type":27,"tag":89,"props":304,"children":306},{"id":305},"_8-使用-typescript-标记-props-和-emit-事件",[307],{"type":32,"value":308},"8. 使用 Typescript 标记 Props 和 Emit 事件",{"type":27,"tag":28,"props":310,"children":311},{},[312],{"type":32,"value":313},"使用 Typescript 类型来清晰的定义 props 和 Emit 事件。",{"type":27,"tag":28,"props":315,"children":316},{},[317,319,325,327],{"type":32,"value":318},"示例:在 Vue3 中可以使用",{"type":27,"tag":106,"props":320,"children":322},{"className":321},[],[323],{"type":32,"value":324},"defineProps",{"type":32,"value":326}," 和 ",{"type":27,"tag":106,"props":328,"children":330},{"className":329},[],[331],{"type":32,"value":332},"defineEmit",{"type":27,"tag":89,"props":334,"children":336},{"id":335},"_9-添加自动格式化插件",[337],{"type":32,"value":338},"9. 添加自动格式化插件",{"type":27,"tag":28,"props":340,"children":341},{},[342],{"type":32,"value":343},"添加 ESLint 或者 Prettier 并使用特定的规则对您的代码进行格式化和质量检查,以增强代码质量和一致性。",{"type":27,"tag":28,"props":345,"children":346},{},[347],{"type":32,"value":348},"示例:将 linting 集成到 CI/CD 中,并自动执行代码审查。",{"type":27,"tag":89,"props":350,"children":352},{"id":351},"_10-在模板template中保持简单",[353],{"type":32,"value":354},"10. 在模板(template)中保持简单",{"type":27,"tag":28,"props":356,"children":357},{},[358],{"type":32,"value":359},"避免在 template 中放置复杂的逻辑,使用计算属性或者方法。",{"type":27,"tag":28,"props":361,"children":362},{},[363,365,371],{"type":32,"value":364},"示例:将 ",{"type":27,"tag":106,"props":366,"children":368},{"className":367},[],[369],{"type":32,"value":370},"v-if=\"list.filter(item => item.active).length > 0\"",{"type":32,"value":372},"的逻辑替换为计算属性。",{"type":27,"tag":89,"props":374,"children":376},{"id":375},"总结",[377],{"type":32,"value":375},{"type":27,"tag":28,"props":379,"children":380},{},[381],{"type":32,"value":382},"通过遵循这些技巧,您将创建更易于维护、扩展和可调试的 Vue.js 应用程序。干净的代码不仅有利于当前的项目,也有利于未来可能维护该项目的开发人员。今天就开始做起,试试其中的一些做法。",{"type":27,"tag":28,"props":384,"children":385},{},[386],{"type":32,"value":387},"保重,下次再见!",{"type":27,"tag":28,"props":389,"children":390},{},[391],{"type":32,"value":392},"一如既往的保持愉快的心情来编码吧!",{"title":8,"searchDepth":394,"depth":394,"links":395},4,[396,398,399,400,401,402,403,404,405,406,407],{"id":91,"depth":397,"text":94},2,{"id":116,"depth":397,"text":119},{"id":170,"depth":397,"text":173},{"id":201,"depth":397,"text":204},{"id":233,"depth":397,"text":236},{"id":257,"depth":397,"text":260},{"id":273,"depth":397,"text":276},{"id":305,"depth":397,"text":308},{"id":335,"depth":397,"text":338},{"id":351,"depth":397,"text":354},{"id":375,"depth":397,"text":375},"markdown","content:posts:2024:vue-cleaner-vue-code.md","content","posts/2024/vue-cleaner-vue-code.md","posts/2024/vue-cleaner-vue-code","md","/posts",[416,420],{"_path":417,"title":418,"date":419},"/2024/rsbuild-create-demo","Rsbuild创建项目","2024-12-08T02:08:01.000Z",{"_path":421,"title":422,"date":423},"/2024/vue-good-practices-and-design-patterns-for-vue-composables","翻译:Vue 可组合项的良好实践和设计模式","2024-12-15T05:38:52.000Z",["Reactive",425],{"$scolor-mode":426,"$ssite-config":429},{"preference":427,"value":427,"unknown":428,"forced":7},"system",true,{"env":430,"name":431,"url":432},"production","月空人","https://www.whbbit.cn",["Set"],["ShallowReactive",435],{"/2024/vue-cleaner-vue-code":-1,"surround-/2024/vue-cleaner-vue-code":-1},{"layout":437,"search":445,"popover":451},{"open":438,"asideItems":441},["Ref",439],["Reactive",440],{"sidebar":7,"aside":7,"search":7},["Ref",442],["Reactive",443],[444],"toc",{"word":446,"result":448},["EmptyRef",447],"\"\"",["Ref",449],["Reactive",450],[],{"pops":452},["Ref",453],["Reactive",454],[]]