uni-app开发微信小程序 vue3写法添加pinia

说明

使用uni-app开发,选择vue3语法,开发工具是HBliuderX。虽然内置有vuex,但是个人还是喜欢用Pinia,所以就添加进去了。
Pinia官网连接

添加步骤

第一步:

在项目根目录下执行命令:

npm install pinia

第二步:

配置main.js文件

// #ifdef VUE3
import { createSSRApp } from 'vue'
import * as Pinia from 'pinia';		// 配置pinia第一句
export function createApp() {
  const app = createSSRApp(App)
  // 状态管理
  const store = Pinia.createPinia();	// 配置pinia第二句
  // 持久化
  app.use(store);						// 配置pinia第三句
  return {
    app,
	Pinia								// 配置pinia第四句
  }
}
// #endif

第三步,使用:

创建store文件夹、创建store/index.js

import {
	appStore
} from "./modules/app.js"

const useStore = () => ({
	app: appStore(),
});

export default useStore;
/**
 * 用法
 * 	import useStore from "@/store/index.js"
	const {
		app
	} = userStore();
	
	let app = app.appIndex
 */

然后创建store/modules/app.js文件

import {
	defineStore
} from 'pinia'
export const appStore = defineStore('app', {
	unistorage: true, // 是否持久化到内存
	state: () => {
		return {
			// 测试
			appIndex: 999,
		}
	},
	actions: {}
})

像下面这个样子:
在这里插入图片描述

使用:

在js文件夹下导入使用即可

import useStore from "@/store/index.js"
	const {
		app
	} = userStore();
	
	let app = app.appIndex

完整一点的示例:

<script>
	import useStore from "@/store/index.js"
	const {app} = useStore();
	export default {
		data() {
			return {
				text:"",
			}
		},
		methods: {
			getText(){
				this.text = app.appIndex;
			}
	}
</script>

如果有更好的方法,欢迎大家一起讨论!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值