vue3:组合式API和选项式API里分别如何使用store

vue3越来越主流了,但是很多人还不习惯vue3的组合式API写法,依旧喜欢用选项是API,但是很多功能的写法是不同的,比如我今天要分享的store写法。

我用的store是pinia。

选项式API(script里不带setup)的写法:

在setup里将store实例化,然后其他地方用this调用即可,watch里不需要加this。

<script>
	import useDesignerStore from '@/store/modules/designer';
	export default {
		name: 'cpt-button',
		setup() {
			const designerStore = useDesignerStore();
			return {
				designerStore
			}
		},
		watch: {
			'designerStore.refreshCptData': {
				handler() {
					this.designerStore.clearCurrentCpt()
				}
			}
		},

		created() {
			this.designerStore.setRefreshCptData();
		}
	}
</script>

组合式API(script里带setup)的写法:

<script setup>
import useDesignerStore from '@/store/modules/designer';
const designerStore = useDesignerStore();
watch(
  () => designerStore.refreshCptData,
  () => {
    designerStore.clearCurrentCpt()
  }
)
const setRefreshCptData = ()=>{
	designerStore.setRefreshCptData()
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值