学无先后,达者为师

网站首页 Vue 正文

vue3 中。我的useStore 一直获取到的是undefiend

作者:yunchong_zhao 更新时间: 2022-04-17 Vue

这个现象奇怪的很。
我从vuex中。结构出来的 useStore 获取出来的 store 一直为空

因为我的vuex中的方法 一直没有触发。最后发现 setup中的store一直为undefined

有的网友说我并没有写在 setup 但是事实上我就是写在setup中

其实原因是 我之前写了一段 需要dom操作 然后放在 setup中

然后就是这段代码导致 其实如果你要在 setup操作dom的话 最好还是

import { nextTick } from "vue"
import { useStore } from "vuex"
setup() {
	const store = useStore()
	// 操作dom的代码
	console.log(store, '====') // undefined ====
}

setup() {
	const store = useStore()
	nextTick(() => {
		// 这里操作dom
	})
	console.log(store, '====') // 输出了store 对象
}



当然 useStore 还得记得在setup中使用哦 这个也要记住。

才一个小坑

关注我 持续更新前端 知识。

原文链接:https://yunchong.blog.csdn.net/article/details/123934976

栏目分类
最近更新