vue.esm.js:1741 TypeError: Cannot read properties of undefined (reading 'push')
时间: 2023-10-19 17:31:58 浏览: 221
vue中渲染对象中属性时显示未定义的解决
5星 · 资源好评率100%
根据提供的引用内容,出现错误"TypeError: Cannot read properties of undefined (reading 'push')"的原因是在代码中使用了未定义的`router.push`方法。这个错误可能是因为`router`对象未正确初始化导致的。
在提供的代码中,`router`对象是在`logout`函数内部定义的,而不是在函数外部定义。因此,当调用`router.push('/login')`时,`router`对象是未定义的,从而导致了错误的发生。正确的代码应该将`router`对象的定义移到函数外部,确保其正确初始化。
以下是修改后的代码示例:
```javascript
const store = useUserStore()
const router = useRouter()
const logout = async () => {
// 弹出框提示
await showConfirmDialog({
title: '温馨提示',
message: '您确定要退出当前账号吗?'
})
store.delUserInfo()
router.push('/login')
}
```
通过将`router`对象的定义移动到函数外部,确保了`router`对象的正确初始化,从而避免了"TypeError: Cannot read properties of undefined (reading 'push')"错误的发生。<span class="em">1</span><span class="em">2</span>
#### 引用[.reference_title]
- *1* [Sawyer-zh#study-note#Vue-js路由报错TypeError-Cannot-read-property-c-](https://download.csdn.net/download/weixin_35829279/86254477)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* [使用vue-router出现Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ...](https://blog.csdn.net/qq_41964720/article/details/129620750)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文