目录
- vue3之vite创建H5项目之3 ( )
-
- 1:Pinia 状态管理
-
- 1-1 之 store / index.ts
- 1-2 之 main.ts引入
- 1-3 之 stores / user.ts
- 1-4 之 stores / app.ts
- 1-5 调用pinia的方法
- 3:后端开启一个服务与axios请求数据
-
- 3-1 ts-node 文件 后端服务
-
- 3-1-1 安装
- 3-1-2 服务的 index.ts
- 3-2 前端使用axios返回接口
- 4 proxy代理 vite.config.ts
- 4:封装axios
vue3之vite创建H5项目之3 ( )
1:Pinia 状态管理
- 安装依赖
pnpm i pinia-plugin-persistedstate pnpm i pinia
1-1 之 store / index.ts
- 使用了数据持久化
pinia-plugin-persistedstate
import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
const store = createPinia()
store.use(piniaPluginPersistedstate)
export default store;
1-2 之 main.ts引入
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import store from "./stores/index"
const app = createApp(App)
app.use(store)
1-3 之 stores / user.ts
import { defineStore } from 'pinia'
import { useAppStore } from './app'
export const useUserStore = defineStore({
id: 'user',
state: () => {
return {
name: '小小亮',
age: 18
}
},
getters: {
fullName: (state) => {
return state.name + '叼'
}
},
actions: {
updateName(data: any) {
this.name = data
},
updateStateObj(data: any) {
this.$state = data
},
updateAppAndAge(data: any) {
this.age = data
this.updateAppConfig()
},
updateAppConfig() {
const appStore = useAppStore()
appStore.setData('app-update')
}
},
persist: {
key: 'user',
storage: sessionStorage,
}
})
1-4 之 stores / app.ts
import { defineStore } from 'pinia'
export const useAppStore = defineStore({
id: 'app',
state: () => {
return {
config: 'app配置'
}
},
actions: {
setData(data: any) {
console.log('setData-app',data)
this.config = data
}
}
})
1-5 调用pinia的方法
<template>
<div>
PinaIndex
<div>
获取name- {{ name }} <br/>
获取age- {{ age }}<br/>
获取fullName-计算属性- {{ userStore.fullName }}<br/>
获取app的config: {{ appStore.config }}
</div>
<div @click="changeName">
修改name
</div>
<div @click="changeStateObj">
修改State对象
</div>
<div @click="changeAppAndAge">
修改Age和app配置
</div>
<div @click="goBack">返回</div>
</div>
</template>
<script setup lang="ts" name='PinaIndex'>
import { computed } from 'vue'
import { useRouter } from "vue-router"
const router = useRouter()
import { useUserStore } from '@/stores/user';
import { useAppStore } from '@/stores/app';
const userStore = useUserStore()
const appStore = useAppStore()
const name = computed(()=>{
return userStore.name
})
const age = computed(()=>{
return userStore.age
})
const changeName = ()=> {
userStore.updateName('xxxx')
}
const changeStateObj = ()=> {
const { name, age } = userStore.$state
userStore.updateStateObj({
name: name + 1,
age: age + 1
})
}
const changeAppAndAge =()=> {
const { age } = userStore.$state
userStore.updateAppAndAge(age+1)
}
const goBack = ()=>{
router.push("/")
}
</script>
<style lang="scss" scoped>
</style>
3:后端开启一个服务与axios请求数据
3-1 ts-node 文件 后端服务
3-1-1 安装
yarn add ts-node -g
npm init -y
yarn add @types/node -D
yarn add express -S
yarn add @types/express -D
yarn add @types/express -D
yarn add typescript -g
3-1-2 服务的 index.ts
import express,{Express,Router,Request,Response} from "express"
import axios from "axios"
const cors = require('cors')
const app:Express = express()
const router:Router = express.Router()
app.use(cors())
app.use("/api",router)
app.get('/list', (req, res) => {
res.set('Content-Type', 'text/plain')
res.send({
code:'200',
data:{
msg:'msg'
}
})
})
app.listen(8989,()=>{
console.log("success server http://localhost:8989");
})
3-2 前端使用axios返回接口
import axios from "axios"
onMounted(async ()=>{
let {data} = await axios.get('http://localhost:8989/list')
console.log('data',data);
})
4 proxy代理 vite.config.ts
export default ({ command }: ConfigEnv): UserConfigExport =>{
return {
server: {
host: '0.0.0.0',
proxy:{
'/api': {
target: 'http://XXXX',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
},
}
},
}
}
4:封装axios