当前位置: 首页 > news >正文

网站加入购物车的代码云主机如何做两个网站

网站加入购物车的代码,云主机如何做两个网站,公司建网站费用怎么做分录,wordpress文章页版权本节重点#xff1a; pinia是什么pinia怎么用 pinia是什么 vue中组件间的数据传递#xff1a; app.config.globalProperties#xff1a;能够被应用内所有组件实例访问到的全局属性的对象props#xff1a;父传子用provide#xff1a;父传后代用 想象下有咩有哪些数据存储… 本节重点 pinia是什么pinia怎么用 pinia是什么 vue中组件间的数据传递 app.config.globalProperties能够被应用内所有组件实例访问到的全局属性的对象props父传子用provide父传后代用 想象下有咩有哪些数据存储是上述没法存的吗一般随着项目越来越复杂一个项目都是有多个菜单组成的例如一个收费系统可以包含入网、客户资料管理、客户收费、组织机构管理、权限角色管理等模块。模块和模块之间或者有的菜单和菜单之间有些数据是共享的例如用户信息、登录用户的权限信息等。这些内容是无法使用上述3块内容的。 Pinia 是Vue的专属状态管理库允许跨组件或页面共享状态。优点 简单易用API 设计简洁直观学习成本低。类型安全在 TypeScript 项目中能提供良好的类型支持。模块化设计方便代码的组织和维护。 使用 安装和初始化 pnpm install pinia // main.js import { createApp } from vue; import { createPinia } from pinia; import App from ./App.vue;const app createApp(App); const pinia createPinia();app.use(pinia); 创建和使用 在项目的 src 目录下创建一个 stores 文件夹用于存放所有的 Store 文件。在 stores 文件夹中创建一个 counter.ts文件。 这个文件结构优点想vue2 的选项式api的模式定义好几个模块什么内容放在什么下面即可。 // src/stores/counter.js import { defineStore } from pinia;export const useCounterStore defineStore(counter, {// 定义状态state: () ({count: 0}),// 定义 getters类似于计算属性getters: {doubleCount: (state) state.count * 2},// 定义 actions用于修改状态actions: {increment() {this.count;}} }); 上述代码中 defineStore 是 Pinia 提供的一个函数用于定义一个 Store命名要求都以use开头store结尾。counter 是 Store 的唯一 id用于区分不同的 Store。state 是一个函数返回一个对象包含了 Store 的初始状态。getters 是一个对象包含了一些计算属性用于获取状态的派生值。actions 是一个对象包含了一些方法用于修改状态。 使用 templatediv!--模板中就可以使用了 --pCount: {{ counterStore.count }}/ppDouble Count: {{ counterStore.doubleCount }}/pbutton clickcounterStore.incrementIncrement/button/div /templatescript setup // 引入了 useCounterStore 函数 import { useCounterStore } from ../stores/counter;// 调用 useCounterStore 函数获取 counter Store 的实例 const counterStore useCounterStore(); /script 访问其他store // src/stores/counter.js import { defineStore } from pinia; import { useAuthStore } from ./auth-storeexport const useCounterStore defineStore(counter, {// 定义状态state: () ({count: 0,preferences: null,}),// 定义 getters类似于计算属性getters: {doubleCount: (state) state.count * 2},// 定义 actions用于修改状态actions: {increment() {this.count;},async fetchUserPreferences() {const auth useAuthStore()if (auth.isAuthenticated) {this.preferences await fetchPreferences()} else {throw new Error(User must be authenticated)}},} }); 响应式解构 从 Store 中解构出状态时默认情况下会失去响应式。为了保持响应式可以使用 storeToRefs 函数 templatedivpCount: {{ count }}/pbutton clickincrementIncrement/button/div /templatescript setup import { useCounterStore } from ../stores/counter; import { storeToRefs } from pinia;const counterStore useCounterStore(); const { count } storeToRefs(counterStore); const { increment } counterStore; /script 项目中的最佳实践 模块化设计 将不同业务模块的状态分别存放在不同的 Store 文件中例如用户信息存放在 user.js 中商品信息存放在 product.js 中这样可以提高代码的可维护性。 合理使用 getters 和 actions getters用于获取状态的派生值避免在组件中重复计算。 actions用于修改状态将复杂的业务逻辑封装在 actions 中使组件的代码更加简洁。 状态持久化 在实际项目中有些状态需要持久化比如用户的登录状态。可以使用 pinia-plugin-persistedstate 插件来实现状态的持久化 pnpm install pinia-plugin-persistedstate // main.js import { createApp } from vue; import { createPinia } from pinia; import piniaPluginPersistedstate from pinia-plugin-persistedstate; import App from ./App.vue;const app createApp(App); const pinia createPinia(); pinia.use(piniaPluginPersistedstate);app.use(pinia); app.mount(#app); 然后在 Store 中配置持久化 // src/stores/counter.js import { defineStore } from pinia; import { useAuthStore } from ./auth-storeexport const useCounterStore defineStore(counter, {// 定义状态state: () ({count: 0,preferences: null,}),// 定义 getters类似于计算属性getters: {doubleCount: (state) state.count * 2},// 定义 actions用于修改状态actions: {increment() {this.count;},async fetchUserPreferences() {const auth useAuthStore()if (auth.isAuthenticated) {this.preferences await fetchPreferences()} else {throw new Error(User must be authenticated)}},},// 配置持久化persist: true }); 继续下一节零基础Vue入门7——状态管理Pinia-CSDN博客 若碰到其他的问题 可以私信我 一起探讨学习 如果对你有所帮助还请 点赞 收藏 谢谢~ 关注收藏博客 持续更新中欢迎订阅专栏
http://www.hkea.cn/news/14390873/

相关文章:

  • 做百度推广需要有网站吗工业园做网站的公司
  • seo站外推广海外电商平台有哪些
  • 南宁做企业网站运营企业网站
  • 网页界面设计内容分站城市网站如何做seo
  • 网站建设分金手指排名八广德做网站设计开发
  • 赵县住房和城乡建设局网站首页竞价sem培训
  • 百度快照 查看指定网站wordpress 不同站点
  • 长春网站建设公司怎么样抖音 运营
  • 做网站内容管理器要吗针织外贸公司
  • 带屏蔽的网站做水晶头seo关键词是什么意思
  • win7 发布asp网站哪个公司的app软件定制
  • 做企业网站的哪家好wordpress 不显示菜单
  • 网站设计建网站2023今天的新闻联播
  • 便民类网站 做wordpress 手册插件
  • 用群晖nas做网站南山做网站
  • 广州高档网站建设网站设计框架
  • 网站开发流程及详解站长工具seo
  • 下载wix做的网站织梦网站开通在线投稿
  • 网站建设经费预算包括哪些wordpress 翻译
  • 余姚市城乡建设局网站cms系统搭建
  • 个人建网站一般多少钱?中国建设银行门户网站企业
  • 网站分析步骤哪个网站可以学做咸菜
  • 哪有做网站的宁波外贸网站建设和推广
  • 网站建设运营执行方案怎么做ppt教程网站
  • 山东网站建设好不好手机网站怎么做沉浸式
  • 哪个网站做推广做的最好超级搜索引擎
  • 东莞网站建设 鞋材厂wordpress页面移动端
  • 过年做哪个网站能致富网络投票程序
  • 网站风格趋势品牌推广与传播怎么写
  • 成都防疫政策最新北京seo排名外包