useDialog()方法在自定义js中返回undefined
ICanThis opened this issue · comments
ICanThis commented
描述错误
使用对话框的时候,我想将它封装为一个js,但是在调用的时候报错了,打印dialog为undefined
复现步骤
import http from './http';
import {useMessage, useDialog} from 'naive-ui';
const msg = useMessage();
const dialog = useDialog();
console.log(dialog)
export function getUserInfo() {
return http.post('/user/info');
}
export function logout() {
return new Promise((res, rej) => {
dialog.warning({
title: '提醒',
content: '确定要退出吗?',
onPositiveClick() {
http.post('/user/logout').then(function (e) {
if (e.code === 200) {
res(e);
msg.success('已退出登录。');
setTimeout(function () {
location.reload();
}, 500);
} else {
rej(e);
msg.error(e.msg);
}
}).catch(e => rej(e));
},
onNegativeClick() {
msg.info('取消退出。');
}
})
});
}
最小复现链接
系统信息
mac, chrome, node v20.12.0
使用的包管理器
npm
验证
ICanThis commented
尝试使用export default {},export default class xxx{}也不能使用, useMessage()返回的也是undefined,我该怎么去在自定义的js中获取到msg和dialog对象
ICanThis commented
并且在组件内部使用的dialog和message是没有问题的
ICanThis commented
找了一下文档,解决了,使用createDiscreteApi,创建独立出来的dialog和message就可以了
jianweiqin commented
找了一下文档,解决了,使用createDiscreteApi,创建独立出来的dialog和message就可以了
可以给一些代码吗?找了一下没看到createDiscreteApi