This commit is contained in:
parent
e8e80f14b1
commit
0937dc0eac
|
|
@ -0,0 +1,23 @@
|
||||||
|
import {defineStore} from 'pinia'
|
||||||
|
|
||||||
|
|
||||||
|
export const useProductStore = defineStore('product', {
|
||||||
|
state: () => ({
|
||||||
|
products: [],
|
||||||
|
total: 0,
|
||||||
|
}),
|
||||||
|
actions: {
|
||||||
|
productSet(data,total) {
|
||||||
|
this.products = data;
|
||||||
|
// this.products.push(...data); //追加数据
|
||||||
|
this.total=total; //这里会更新
|
||||||
|
},
|
||||||
|
productTotalSet(total){
|
||||||
|
this.total=total
|
||||||
|
}
|
||||||
|
},
|
||||||
|
getters: {
|
||||||
|
count: (state) => state.products.length,
|
||||||
|
total: (state) => state.total,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
<template>
|
<template>
|
||||||
<el-form :model="form" label-width="auto" style="max-width: 600px">
|
<el-form :model="form" label-width="auto" style="max-width: 600px">
|
||||||
<el-form-item label="Activity name">
|
<el-form-item label="Activity name">
|
||||||
<el-input v-model="form.name" />
|
<el-input v-model=" productStore.count" />
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="Activity zone">
|
<el-form-item label="Activity zone">
|
||||||
<el-select v-model="form.region" placeholder="please select your zone">
|
<el-select v-model="form.region" placeholder="please select your zone">
|
||||||
|
|
@ -66,14 +66,14 @@
|
||||||
</template>
|
</template>
|
||||||
<div>
|
<div>
|
||||||
<p>hello about</p>
|
<p>hello about</p>
|
||||||
<p>Count: {{ tokenStore.count }}</p>
|
<p>Count: {{ productStore.count }}</p>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { useTokenStore } from '../store/token.js';
|
import { useProductStore } from '../store/product.js';
|
||||||
|
|
||||||
const tokenStore=useTokenStore();
|
const productStore=useProductStore();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
<p>hello home</p>
|
<p>hello home</p>
|
||||||
<p>Count: {{ tokenStore.count }}</p>
|
<p>Count: {{ productStore.count}}</p>
|
||||||
<button @click="tokenStore.increment">Increment</button>
|
|
||||||
<button @click="tokenStore.incrementAsync">Increment Async</button>
|
|
||||||
<router-link to="/about">About</router-link>
|
<router-link to="/about">About</router-link>
|
||||||
<el-alert
|
<el-alert
|
||||||
v-if="showAlert"
|
v-if="showAlert"
|
||||||
|
|
@ -19,26 +17,27 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref } from 'vue';
|
import { ref, onMounted } from 'vue';
|
||||||
|
import { storeToRefs } from 'pinia';
|
||||||
import request from '../api/request.js'
|
import request from '../api/request.js'
|
||||||
import { useTokenStore } from '../store/token.js';
|
import { useProductStore } from '../store/product.js';
|
||||||
|
|
||||||
const tokenStore=useTokenStore()
|
|
||||||
|
|
||||||
// 定义用于控制 el-alert 显示的变量
|
// 定义用于控制 el-alert 显示的变量
|
||||||
const showAlert = ref(false);
|
const showAlert = ref(false);
|
||||||
const alertTitle = ref('');
|
const alertTitle = ref('');
|
||||||
const alertType = ref('warning');
|
const alertType = ref('warning');
|
||||||
const alertDescription = ref('');
|
const alertDescription = ref('');
|
||||||
|
const productStore=useProductStore();
|
||||||
const fetchData = async()=>{
|
const fetchData = async()=>{
|
||||||
try {
|
try {
|
||||||
const response=await request.get('/api/product')
|
const response=await request.get('/api/product');
|
||||||
console.log(response)
|
productStore.productSet(response.data,100);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alertTitle.value = 'Error';
|
console.log(error);
|
||||||
alertDescription.value = error.message;
|
|
||||||
showAlert.value = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onMounted(()=>{
|
||||||
|
fetchData()
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
Loading…
Reference in New Issue