parent
217a087d64
commit
16e610dbae
12 changed files with 218 additions and 1 deletions
@ -0,0 +1,5 @@ |
|||||||
|
//创建大仓库
|
||||||
|
import {createPinia} from "pinia"; |
||||||
|
let store = createPinia(); |
||||||
|
//createPinia方法可以用于创建大仓库
|
||||||
|
export default store; |
@ -0,0 +1,32 @@ |
|||||||
|
//定义info小仓库,defineStore用于小仓库
|
||||||
|
import {defineStore} from "pinia"; |
||||||
|
//第一个仓库:小仓库名字 第二个参数:小仓库配置对象
|
||||||
|
//defineStroe方法执行会返回一个函数,函数作用就是让组件可以获取到仓库数据
|
||||||
|
let useInfoStore = defineStore('info',{ |
||||||
|
//存储数据:state
|
||||||
|
state:()=>{ |
||||||
|
return{ |
||||||
|
count:99, |
||||||
|
arr:[1,2,3,4,5,6,7,8,9,10] |
||||||
|
} |
||||||
|
}, |
||||||
|
//操作方法actions
|
||||||
|
actions:{ |
||||||
|
// 注意:函数没有context上下文对象
|
||||||
|
// 没有commit、没有mutations去修改数据
|
||||||
|
updateNum(a:number,b:number){ |
||||||
|
this.count += a; |
||||||
|
} |
||||||
|
}, |
||||||
|
//计算属性getters
|
||||||
|
getters:{ |
||||||
|
total(){ |
||||||
|
let result = this.arr.reduce((prev:number,next:number)=>{ |
||||||
|
return prev + next |
||||||
|
},0); |
||||||
|
return result; |
||||||
|
} |
||||||
|
}, |
||||||
|
|
||||||
|
}); |
||||||
|
export default useInfoStore; |
@ -0,0 +1,31 @@ |
|||||||
|
<template> |
||||||
|
<div class="box1"> |
||||||
|
<h1>{{infoStore.count}}-------{{infoStore.total}}</h1> |
||||||
|
<el-button type="primary" @click="updateCount">点我修改仓库数据</el-button> |
||||||
|
</div> |
||||||
|
</template> |
||||||
|
|
||||||
|
<script setup lang="ts"> |
||||||
|
import useInfoStore from "@/store/modules/info"; |
||||||
|
//获取小仓库对象 |
||||||
|
let infoStore = useInfoStore(); |
||||||
|
|
||||||
|
const updateCount = ()=>{ |
||||||
|
// infoStore.count ++ |
||||||
|
|
||||||
|
//$patch方法 |
||||||
|
// infoStore.$patch({ |
||||||
|
// count:111 |
||||||
|
// }) |
||||||
|
infoStore.updateNum(10,100) |
||||||
|
console.log(infoStore,'========infoStore信息') |
||||||
|
} |
||||||
|
</script> |
||||||
|
|
||||||
|
<style scoped> |
||||||
|
.box1{ |
||||||
|
width: 300px; |
||||||
|
height: 200px; |
||||||
|
background: darkorange; |
||||||
|
} |
||||||
|
</style> |
@ -0,0 +1,19 @@ |
|||||||
|
<template> |
||||||
|
<div class="box2"> |
||||||
|
<h1>{{useInfo.count}}+++++++{{useInfo.total}}</h1> |
||||||
|
</div> |
||||||
|
</template> |
||||||
|
|
||||||
|
<script setup lang="ts"> |
||||||
|
import useInfoStore from "@/store/modules/info"; |
||||||
|
let useInfo = useInfoStore(); |
||||||
|
|
||||||
|
</script> |
||||||
|
|
||||||
|
<style scoped> |
||||||
|
.box2{ |
||||||
|
width: 300px; |
||||||
|
height: 200px; |
||||||
|
background: lightgoldenrodyellow; |
||||||
|
} |
||||||
|
</style> |
@ -0,0 +1,31 @@ |
|||||||
|
<template> |
||||||
|
<div class="box"> |
||||||
|
<h2>pinia</h2> |
||||||
|
<hr> |
||||||
|
<div class="container"> |
||||||
|
<child-a></child-a> |
||||||
|
<child-b></child-b> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</template> |
||||||
|
|
||||||
|
<script setup lang="ts"> |
||||||
|
import ChildA from "@/views/pinia/component/child-A.vue"; |
||||||
|
import ChildB from "@/views/pinia/component/child-B.vue"; |
||||||
|
// vuex:集中式管理状态容器,可以实现任意组件之间通信!!! |
||||||
|
// 核心概念:state、mutations、actions、getters、modules |
||||||
|
// pinia:集中式管理状态容器,可以实现任意组件之间通信!!! |
||||||
|
// 核心概念:state、actions、getters |
||||||
|
// pinia写法:选择器API、组合式API |
||||||
|
</script> |
||||||
|
|
||||||
|
<style scoped> |
||||||
|
.box{ |
||||||
|
width: 600px; |
||||||
|
height: 300px; |
||||||
|
background: powderblue; |
||||||
|
} |
||||||
|
.container{ |
||||||
|
display: flex; |
||||||
|
} |
||||||
|
</style> |
@ -0,0 +1,17 @@ |
|||||||
|
<template> |
||||||
|
<div class="box2"> |
||||||
|
<h2>grandson</h2> |
||||||
|
</div> |
||||||
|
</template> |
||||||
|
|
||||||
|
<script setup lang="ts"> |
||||||
|
|
||||||
|
</script> |
||||||
|
|
||||||
|
<style scoped> |
||||||
|
.box2{ |
||||||
|
width: 100px; |
||||||
|
height: 200px; |
||||||
|
background: darkseagreen; |
||||||
|
} |
||||||
|
</style> |
@ -0,0 +1,27 @@ |
|||||||
|
<template> |
||||||
|
<div class="box1"> |
||||||
|
<h2>son</h2> |
||||||
|
<hr> |
||||||
|
<p>{{carName}}</p> |
||||||
|
<el-button type="success" @click="updateCar">更新数据</el-button> |
||||||
|
<grandson></grandson> |
||||||
|
</div> |
||||||
|
</template> |
||||||
|
|
||||||
|
<script setup lang="ts"> |
||||||
|
import {ref,inject} from "vue"; |
||||||
|
import Grandson from "@/views/provide-inject/component/grandson.vue"; |
||||||
|
let carName = ref(inject('TOKEN').value) |
||||||
|
// console.log(carName,'获取的inject数据') |
||||||
|
const updateCar = ()=>{ |
||||||
|
carName.value = 'passt' |
||||||
|
} |
||||||
|
</script> |
||||||
|
|
||||||
|
<style scoped> |
||||||
|
.box1{ |
||||||
|
width: 200px; |
||||||
|
height: 300px; |
||||||
|
background: #f2f2f2; |
||||||
|
} |
||||||
|
</style> |
@ -0,0 +1,26 @@ |
|||||||
|
<template> |
||||||
|
<div class="box"> |
||||||
|
<h1>provide和inject</h1> |
||||||
|
<hr> |
||||||
|
<son></son> |
||||||
|
</div> |
||||||
|
</template> |
||||||
|
|
||||||
|
<script setup lang="ts"> |
||||||
|
import Son from "@/views/provide-inject/component/son.vue"; |
||||||
|
//vue3提供provide(提供)与inject(注入),可以实现隔辈组件传递数据 |
||||||
|
import {ref,provide} from "vue"; |
||||||
|
let car = ref('HORCH') |
||||||
|
//祖先组件给后代组件提供数据 |
||||||
|
// 两个参数:第一个参数就是提供的数据key |
||||||
|
// 第二个参数:祖先组件提供数据 |
||||||
|
provide("TOKEN",car); |
||||||
|
</script> |
||||||
|
|
||||||
|
<style scoped> |
||||||
|
.box{ |
||||||
|
width: 60vw; |
||||||
|
height: 600px; |
||||||
|
background:powderblue ; |
||||||
|
} |
||||||
|
</style> |
Loading…
Reference in new issue