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()
}

相关推荐

  1. vue3组合API选项API分别如何使用store

    2024-04-27 02:40:03       13 阅读
  2. vue选项API组合Api

    2024-04-27 02:40:03       40 阅读
  3. 选项API组合API

    2024-04-27 02:40:03       38 阅读
  4. vue选项API组合API区别-备忘

    2024-04-27 02:40:03       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-27 02:40:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-27 02:40:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-27 02:40:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-27 02:40:03       18 阅读

热门阅读

  1. leetcode hot100_part25

    2024-04-27 02:40:03       15 阅读
  2. 选购AI智能电话机器人需要注意哪些事项

    2024-04-27 02:40:03       16 阅读
  3. leetcode刷题(python)——(七)

    2024-04-27 02:40:03       14 阅读
  4. AI Python 教程

    2024-04-27 02:40:03       16 阅读
  5. Python编程——常用的Python单行实现功能代码示例

    2024-04-27 02:40:03       15 阅读
  6. TS码流解析(三)PES

    2024-04-27 02:40:03       16 阅读
  7. 五、搭建 Videojs,实战网络直播

    2024-04-27 02:40:03       14 阅读
  8. 【QEMU系统分析之启动篇(十九)】

    2024-04-27 02:40:03       14 阅读
  9. draw.io: 开启图表绘制的无限可能

    2024-04-27 02:40:03       15 阅读
  10. draw.io使用心得

    2024-04-27 02:40:03       15 阅读
  11. draw.io使用心得

    2024-04-27 02:40:03       14 阅读
  12. 机器学习笔记-01

    2024-04-27 02:40:03       10 阅读
  13. 成为程序员后我都明白了什么?

    2024-04-27 02:40:03       15 阅读
  14. 某人的系统四分法(管理+ 规则+应用+对接)

    2024-04-27 02:40:03       12 阅读
  15. pandas分组聚合练习

    2024-04-27 02:40:03       11 阅读