App组件:
- <template>
- <Count></Count>
- <br />
- <LoveTalk></LoveTalk>
- </template>
-
- <script setup lang="ts" name="App">
- import Count from './components/Count.vue'
- import LoveTalk from './components/LoveTalk.vue'
- </script>
-
- <style scoped></style>
Count组件:
- <template>
- <div class="count">
- <h2>当前求和为:{{ sum }}</h2>
- <select v-model.number="n">
- <option value="1">1</option>
- <option value="2">2</option>
- <option value="3">3</option>
- </select>
- <button @click="add">加</button>
- <button @click="minus">减</button>
- </div>
- </template>
-
- <script setup lang="ts" name="Count">
- import { ref } from 'vue'
- // 数据
- let sum = ref(1) //当前求和
- let n = ref(1) //用户选择的数据
- // 方法
- function add () {
- sum.value += n.value
- }
- function minus () {
- sum.value -= n.value
- }
- </script>
-
- <style scoped>
- .count {
- padding: 10px;
- border-radius: 10px;
- box-shadow: 0 0 10px;
- background-color: skyblue;
- }
- select,
- button {
- margin: 0px 5px;
- height: 25px;
- }
- </style>
LoveTalk组件:
- <template>
- <div class="talk">
- <button @click="getLoveTalk">获取一句土味情话</button>
- <ul>
- <li v-for="talk in talkList" :key="talk.id">
- {{ talk.title }}
- </li>
- </ul>
- </div>
- </template>
-
- <script setup lang="ts" name="LoveTalk">
- import { reactive } from 'vue'
- import axios from 'axios'
- import { nanoid } from 'nanoid'
- import { title } from 'process'
- // 数据
- let talkList = reactive([
- {
- id: 'wad1',
- title: '莫文蔚的阴天,孙燕姿的雨天,周杰伦的晴天,都不如你和我聊天。'
- },
- {
- id: 'wad2',
- title: '“你为什么要害我?”“害你什么?”“害我那么喜欢你!”'
- },
- {
- id: 'wad3',
- title: '你知道你和星星有什么区别吗?星星在天上,你在我心里。'
- }
- ])
- // 方法
- async function getLoveTalk () {
- // 发请求
- let result = await axios.get(
- 'https://api.uomg.com/api/rand.qinghua?format=json'
- )
- // 把请求回来的字符串,包装成一个对象
- let obj = { id: nanoid(), title: result.data.content }
- talkList.unshift(obj)
- }
- </script>
- <style scoped>
- .talk {
- padding: 10px;
- border-radius: 10px;
- box-shadow: 0 0 10px;
- background-color: orange;
- }
- select,
- button {
- margin: 0px 5px;
- height: 25px;
- }
- </style>
第一步:npm install pinia
第二步:操作src/main.ts
- import { createApp } from 'vue'
- import App from './App.vue'
-
- /* 引入createPinia,用于创建pinia */
- import { createPinia } from 'pinia'
-
- /* 创建pinia */
- const pinia = createPinia()
- const app = createApp(App)
-
- /* 使用插件 */{}
- app.use(pinia)
- app.mount('#app')
此时开发者工具中已经有了pinia选项
- import { defineStore } from "pinia";
- export const useCountStore = defineStore("count", {
- // 真正存储数据的地方
- state() {
- return {
- sum: 6,
- };
- },
- });
- // 引入defineStore用于创建store
- import { defineStore } from "pinia";
-
- // 定义并暴露一个store
- export const useTalkStore = defineStore("talk", {
- // 动作
- actions: {},
- // 状态
- state() {
- return {
- talkList: [
- {
- id: "wad1",
- title: "莫文蔚的阴天,孙燕姿的雨天,周杰伦的晴天,都不如你和我聊天。",
- },
- {
- id: "wad2",
- title: "“你为什么要害我?”“害你什么?”“害我那么喜欢你!”",
- },
- {
- id: "wad3",
- title: "你知道你和星星有什么区别吗?星星在天上,你在我心里。",
- },
- ],
- };
- },
- });
- <template>
- <div class="count">
- <h2>当前求和为:{{ sumStore.sum }}</h2>
- <select v-model.number="n">
- <option value="1">1</option>
- <option value="2">2</option>
- <option value="3">3</option>
- </select>
- <button @click="add">加</button>
- <button @click="minus">减</button>
- </div>
- </template>
- <script setup lang="ts" name="Count">
- import { ref } from 'vue'
- import { useCountStore } from '@/store/count'
- // 数据
- let sumStore = useCountStore()
- let n = ref(1) //用户选择的数据
- // 方法
- function add () {
- sumStore.sum += n.value
- }
- function minus () {
- sumStore.sum -= n.value
- }
- </script>
- <template>
- <div class="talk">
- <button @click="getLoveTalk">获取一句土味情话</button>
- <ul>
- <li v-for="talk in talkStore.talkList" :key="talk.id">
- {{ talk.title }}
- </li>
- </ul>
- </div>
- </template>
- <script setup lang="ts" name="LoveTalk">
- import { reactive } from 'vue'
- import axios from 'axios'
- import { nanoid } from 'nanoid'
- import { useTalkStore } from '@/store/talk'
- // 数据
- let talkStore = useTalkStore()
- // 方法
- async function getLoveTalk () {
- // 发请求
- let result = await axios.get(
- 'https://api.uomg.com/api/rand.qinghua?format=json'
- )
- // 把请求回来的字符串,包装成一个对象
- let obj = { id: nanoid(), title: result.data.content }
- talkStore.talkList.unshift(obj)
- }
- </script>
- countStore.$patch({
- sum:999,
- school:'atguigu'
- })
- import { defineStore } from 'pinia'
-
- export const useCountStore = defineStore('count', {
- /*************/
- actions: {
- //加
- increment(value:number) {
- if (this.sum < 10) {
- //操作countStore中的sum
- this.sum += value
- }
- },
- //减
- decrement(value:number){
- if(this.sum > 1){
- this.sum -= value
- }
- }
- },
- /*************/
- })
- // 使用countStore
- const countStore = useCountStore()
-
- // 调用对应action
- countStore.incrementOdd(n.value)
- <template>
- <div class="count">
- <h2>当前求和为:{{sum}}</h2>
- </div>
- </template>
-
- <script setup lang="ts" name="Count">
- import { useCountStore } from '@/store/count'
- /* 引入storeToRefs */
- import { storeToRefs } from 'pinia'
-
- /* 得到countStore */
- const countStore = useCountStore()
- /* 使用storeToRefs转换countStore,随后解构 */
- const {sum} = storeToRefs(countStore)
- </script>
- // 引入defineStore用于创建store
- import {defineStore} from 'pinia'
-
- // 定义并暴露一个store
- export const useCountStore = defineStore('count',{
- // 动作
- actions:{
- /************/
- },
- // 状态
- state(){
- return {
- sum:1,
- school:'atguigu'
- }
- },
- // 计算
- getters:{
- bigSum:(state):number => state.sum *10,
- upperSchool():string{
- return this. school.toUpperCase()
- }
- }
- })
- const {increment,decrement} = countStore
- let {sum,school,bigSum,upperSchool} = storeToRefs(countStore)
通过 store 的 $subscribe() 方法侦听 state 及其变化,类似于watch
mutate:本次修改的信息
state:真正修改的数据
- talkStore.$subscribe((mutate, state) => {
- localStorage.setItem('talkList', JSON.stringify(talkList.value))
- })
- import { defineStore } from "pinia";
- import axios from "axios";
- import { nanoid } from "nanoid";
- import { reactive } from "vue";
-
- export const useTalkStore = defineStore("talk", () => {
- // talkList就是state
- const talkList = reactive(
- JSON.parse(localStorage.getItem("talkList") as string) || []
- );
-
- // getATalk函数相当于action
- async function getLoveTalk() {
- // 发请求,下面这行的写法是:连续解构赋值+重命名
- let {
- data: { content: title },
- } = await axios.get("https://api.uomg.com/api/rand.qinghua?format=json");
- // 把请求回来的字符串,包装成一个对象
- let obj = { id: nanoid(), title };
- // 放到数组中
- talkList.unshift(obj);
- }
- return { talkList, getLoveTalk };
- });