vue学习

This commit is contained in:
yingfeng.ai 2024-11-06 13:50:53 +08:00
parent c6490526fe
commit 367f39beed
4 changed files with 66 additions and 11 deletions

View File

@ -1,6 +1,6 @@
<template>
<div class="count">
<h2>当前求和:{{ sum }}</h2>
<h2>当前求和:{{ sum }} 放大10倍 {{ bigSum }}</h2>
<select v-model.number="n">
<option value="1">1</option>
<option value="2">2</option>
@ -13,15 +13,24 @@
<script setup lang="ts">
import {ref} from 'vue'
import {useCountStore} from '@/store/count'
import {storeToRefs} from "pinia"
const countStore = useCountStore()
// pinia
const {sum, bigSum} = storeToRefs(countStore)
console.log(bigSum)
let sum = ref(1)
let n = ref(1)
function add(){
sum.value += n.value
function add() {
countStore.increment(n.value)
}
function minus(){
sum.value -= n.value
function minus() {
countStore.reduce(n.value)
}
</script>

View File

@ -12,12 +12,17 @@ import {reactive, ref} from 'vue'
import axios from "axios";
import { v4 as uuidv4 } from 'uuid'
let talkList = reactive([
{id: 'dasfada001', title: '111ue3入门到实战最新版vue3+TypeScript前端开发教程'},
{id: 'dasfada002', title: '333ue3入门到实战最新版vue3+TypeScript前端开发教程'},
{id: 'dasfada003', title: '222ue3入门到实战最新版vue3+TypeScript前端开发教程'}
])
import { useTalkStore } from "@/store/lovetalk"
const talkStore = useTalkStore()
let talkList = reactive(talkStore.talkList)
// ,稿,,
talkStore.$subscribe((mutate, state) => {
console.log(mutate)
console.log(state)
localStorage.setItem('talkList', JSON.stringify(state.talkList))
})
async function getTitle(){
let res = await axios.get("https://api.uomg.com/api/rand.qinghua?format=json")

View File

@ -0,0 +1,28 @@
import {defineStore} from 'pinia'
export const useCountStore = defineStore('count', {
// 动作函数响应动作
actions: {
increment(val: Number) {
if (this.sum < 10) {
this.sum += val
}
},
reduce(val: Number) {
if (this.sum > -10) {
this.sum -= val
}
},
},
state() {
return {sum: 6}
},
// 放大 函数 state的返回值有点hook的感觉
getters : {
bigSum(state){
return state.sum * 10
}
}
}
)

View File

@ -0,0 +1,13 @@
import { defineStore } from 'pinia';
export const useTalkStore = defineStore('talk', {
state() {
// 获取 localStorage 中的数据,并判断是否存在
const storedTalkList = localStorage.getItem('talkList');
// 如果存储的 talkList 存在,则解析它,否则使用空数组
return {
talkList: storedTalkList ? JSON.parse(storedTalkList) : []
};
}
});