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

开设网站步骤批量查询收录

开设网站步骤,批量查询收录,网站建设技术主管,微信公众号开发教程定义 Store | Pinia 开发文档 1.什么是Pinaia Pinia 是 Vue 的专属状态管理库,它允许你跨组件或页面共享状态。 2.理解Pinaia核心概念 定义Store 在深入研究核心概念之前,我们得知道 Store 是用 defineStore() 定义的,它的第一个参数要求是一…

定义 Store | Pinia

开发文档

1.什么是Pinaia

Pinia 是 Vue 的专属状态管理库,它允许你跨组件或页面共享状态。

2.理解Pinaia核心概念

定义Store

在深入研究核心概念之前,我们得知道 Store 是用 defineStore() 定义的,它的第一个参数要求是一个独一无二的名字:

import { defineStore } from 'pinia'// 你可以对 `defineStore()` 的返回值进行任意命名,但最好使用 store 的名字,同时以 `use` 开头且以 `Store` 结尾。(比如 `useUserStore`,`useCartStore`,`useProductStore`)
// 第一个参数是你的应用中 Store 的唯一 ID。
export const useAlertsStore = defineStore('alerts', {// 其他配置...
})

这个名字 ,也被用作 id ,是必须传入的, Pinia 将用它来连接 store 和 devtools。为了养成习惯性的用法,将返回的函数命名为 use... 是一个符合组合式函数风格的约定。

defineStore() 的第二个参数可接受两类值:Setup 函数或 Option 对象。、

Setup Store 中:

  • ref() 就是 state 属性
  • computed() 就是 getters
  • function() 就是 actions

使用Store

虽然我们前面定义了一个 store,但在我们使用 <script setup> 调用 useStore()(或者使用 setup() 函数,像所有的组件那样) 之前,store 实例是不会被创建的

<script setup>
import { useCounterStore } from '@/stores/counter'
// 可以在组件中的任意位置访问 `store` 变量 ✨
const store = useCounterStore()
</script>

3.状态持久化

方法一 整体添加

main.ts入口文件添加整体持久化方案

import {createPinia} from 'pinia'
const pinia = createPinia()
if(localStorage.getItem("pinia")){
pinia.state.value = JSON.parse(localStorage.getItem("pinia"))
}
watch(pinia.state,state=>{
localStorage.setItem("pinia",JSON.stringify(state))
},{deep:true})
app.use(pinia)

方法二 插件

npm i pinia-plugin-persistedstate --save
import {createPinia} from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)
app.use(pinia)
import {ref, computed, watch} from 'vue'
import {defineStore} from 'pinia'
//counter是状态库id 或名称
export const useCounterStore = defineStore('counter', () => {
//变量 state
const count = ref(0)
//计算属性 getter
const doubleCount = computed(() => count.value * 2)
//方法action
function increment() {
count.value++
}
const user = ref({
name: '',
desc: '一键三连'
})
return {count, doubleCount, increment}
}, {persist: true})

结果添加 ,{persist: true} 就可以持久化了

Pinanad的使用

components/Aa.vue 组件

components/Bb.vue组件

stores/counter.js     选项式的用法

 stores/ token.js     组合式写法

main.js

import { createApp } from 'vue'
import App from './App.vue'
const app = createApp(App)
import { createPinia } from 'pinia'
app.use(createPinia())
app.mount('#app')

App.vue

<script setup>
import {version as v1, ref} from 'vue'
import Bb from "@/components/Bb.vue";
import Aa from "@/components/Aa.vue";
const v2 = ref('2.1.6')
import {useCounterStore} from './stores/counter'
const c = useCounterStore()
import {useTokenStore} from './stores/token'
const us = useTokenStore()
</script><template>{{ v1 }} {{ v2 }}<h1>App</h1><button @click="us.count++">add</button><button @click="us.increment()">increment</button><button @click="c.count++">add</button><button @click="c.increment()">increment</button><p>{{us.count}} ---{{us.doubleCount}}</p><hr><p>{{ c.count }} -- {{ c.doubleCount }}</p><p>{{}}</p><h1>Aa组件</h1><Aa/><h1>BB组件</h1><Bb/></template><style scoped></style>

count.js

import {computed, ref, watch} from 'vue'
import {defineStore} from 'pinia'
export const useCounterStore = defineStore('counter', () => {const count = ref(0)const doubleCount = computed(() => count.value * 2)function increment() {count.value++}return {count, doubleCount, increment}
})

token.js


import {defineStore} from 'pinia'export const useTokenStore = defineStore('token', {state: () => {return {count: 0,}},getters: {doubleCount: state => state.count * 2},actions: {increment() {this.count++}},
})

Aa.vue

<script setup>
import {useCounterStore} from '../stores/counter.js'const cc = useCounterStore()import {useTokenStore} from '../stores/token'const us = useTokenStore()</script><template><h1>Aaaaaa</h1><p>Aaa : {{ cc.count }} -- {{ cc.doubleCount }}</p><button @click="cc.count+=5">Aa add +=5</button><p>Aaa : {{ us.count }} -- {{ us.doubleCount }}</p><button @click="us.count+=5">Aa add +=5</button>
</template><style scoped></style>

Ba.vue

<script setup>
import {useCounterStore} from '../stores/counter.js'
const cc = useCounterStore()
</script><template>
<h1>Bb</h1>
<p>Bb : {{cc.count}}  -- {{cc.doubleCount}}</p>
</template><style scoped></style>

http://www.jinmujx.cn/news/109828.html

相关文章:

  • 南京网上注册公司流程网络推广seo教程
  • 创建个人网站教程厦门专业做优化的公司
  • 6.网站开发流程是什么google搜索引擎优化
  • wordpress5.2添加外链网站优化排名软件推广
  • 网站建设公司的性质天津seo顾问
  • iframe框架做网站网站推广策划书范文
  • 网站制作详细流程怎么制作网页教程
  • 网站如何做中英文双语言版本上海最新发布最新
  • 南宁定制网站建设影响seo排名的因素
  • 一了网站萧山区seo关键词排名
  • 网站开发设计的论文南宁网络推广软件
  • 在哪里建立个人网站搜一搜百度
  • 网站建设关键词优化西安网站建设方案优化
  • 郑州关键词seo百度视频排名优化
  • 做网站公司联系方式页面免费打广告平台有哪些
  • 深圳专业做网站建设深圳网络推广服务是什么
  • 做游戏ppt下载网站有哪些内容百度推广开户怎么开
  • 攀枝花建设工程有限责任公司网站网站优化关键词
  • wordpress模板 导购专业做seo推广
  • 旅游网页设计模板图及代码seo推广官网
  • 做的很好的黑白网站郑州网络营销推广
  • wordpress 底部样式湖南网站seo公司
  • 网站维护收费杭州seo关键字优化
  • 教育咨询网站模板seo搜索引擎
  • ui设计难吗seo整站优化费用
  • 免费建站并且绑定域名优化大师专业版
  • 软件下载网站哪个最安全搜索引擎优化百度百科
  • 是想建个网站 用本地做服务器上饶seo博客
  • 微信小程序第三方开发对网站进行seo优化
  • 舟山市住房城乡建设局网站中央新闻今日要闻