通用方法
通用方法
注意
以下示例代码 this.$xxx 方法在 Vue3 中对应 proxy.$xxx 方法,其中 proxy 需要在script标签中定义
const { proxy } = getCurrentInstance()
$tab 对象
$tab对象用于做页签操作、刷新页签、关闭页签、打开页签、修改页签等,它定义在 plugins/tab.js 文件中,它有如下方法
- 打开页签
// 只接受一个参数:路径字符串或 route 对象
proxy.$tab.openPage('/system/user')
// 带查询参数
proxy.$tab.openPage({ path: '/system/user', query: { id: 1 } })
参数只有一个
openPage(url) 内部是 router.push(url),没有标题参数。不要写成 openPage('用户管理', '/system/user')——第二个参数会被忽略,实际跳转的是字符串"用户管理"。
- 修改页签
const obj = Object.assign({}, this.$route, { title: "自定义标题" });
this.$tab.updatePage(obj);
this.$tab.updatePage(obj).then(() => {
// 执行结束的逻辑
});
- 关闭页签
// 关闭当前tab页签,打开新页签
const obj = { path: "/system/user" };
this.$tab.closeOpenPage(obj);
// 关闭当前页签,回到首页
this.$tab.closePage();
// 关闭指定页签
const obj = { path: "/system/user", name: "User" };
this.$tab.closePage(obj);
this.$tab.closePage(obj).then(() => {
// 执行结束的逻辑
});
- 刷新页签
// 刷新当前页签
this.$tab.refreshPage();
// 刷新指定页签
const obj = { path: "/system/user", name: "User" };
this.$tab.refreshPage(obj);
this.$tab.refreshPage(obj).then(() => {
// 执行结束的逻辑
});
- 关闭所有页签
this.$tab.closeAllPage();
this.$tab.closeAllPage().then(() => {
// 执行结束的逻辑
});
- 关闭左侧页签
this.$tab.closeLeftPage();
const obj = { path: "/system/user", name: "User" };
this.$tab.closeLeftPage(obj);
this.$tab.closeLeftPage(obj).then(() => {
// 执行结束的逻辑
});
- 关闭右侧页签
this.$tab.closeRightPage();
const obj = { path: "/system/user", name: "User" };
this.$tab.closeRightPage(obj);
this.$tab.closeRightPage(obj).then(() => {
// 执行结束的逻辑
});
- 关闭其他 tab 页签
this.$tab.closeOtherPage();
const obj = { path: "/system/user", name: "User" };
this.$tab.closeOtherPage(obj);
this.$tab.closeOtherPage(obj).then(() => {
// 执行结束的逻辑
});
$modal 对象
$modal对象用于做消息提示、通知提示、对话框提醒、二次确认、遮罩等,它定义在 plugins/modal.js 文件中,它有如下方法
- 提供成功、警告和错误等反馈信息
this.$modal.msg("默认反馈");
this.$modal.msgError("错误反馈");
this.$modal.msgSuccess("成功反馈");
this.$modal.msgWarning("警告反馈");
提示
其他使用看文件 plugins/modal.js
$auth 对象
$auth对象用于验证用户是否拥有某(些)权限或角色,它定义在 plugins/auth.js 文件中,它有如下方法
- 验证用户权限
// 验证用户是否具备某权限
this.$auth.hasPermi("system:user:add");
// 验证用户是否含有指定权限,只需包含其中一个
this.$auth.hasPermiOr(["system:user:add", "system:user:update"]);
// 验证用户是否含有指定权限,必须全部拥有
this.$auth.hasPermiAnd(["system:user:add", "system:user:update"]);
$cache 对象
$cache对象用于处理缓存。我们并不建议您直接使用sessionStorage或localStorage,因为项目的缓存策略可能发生变化,通过$cache 对象做一层调用代理则是一个不错的选择。$cache 提供 session 和 local 两种级别的缓存,如下:
| 对象名称 | 缓存对象 |
|---|---|
| session | 会话级缓存,通过 sessionStorage 实现 |
| local | 本地级缓存,通过 localStorage 实现 |
- 示例
// local 普通值
this.$cache.local.set("key", "local value");
console.log(this.$cache.local.get("key")); // 输出'local value'
// session 普通值
this.$cache.session.set("key", "session value");
console.log(this.$cache.session.get("key")); // 输出'session value'
// local JSON值
this.$cache.local.setJSON("jsonKey", { localProp: 1 });
console.log(this.$cache.local.getJSON("jsonKey")); // 输出'{localProp: 1}'
// session JSON值
this.$cache.session.setJSON("jsonKey", { sessionProp: 1 });
console.log(this.$cache.session.getJSON("jsonKey")); // 输出'{sessionProp: 1}'
// 删除值
this.$cache.local.remove("key");
this.$cache.session.remove("key");
$download 对象
$download 用于按后端约定下载文件(自动带鉴权头、校验是否登录失效、从响应头取文件名),定义在 plugins/download.js:
// 1) 按文件名下载服务器上的文件(delete=true 表示下载后删除临时文件)
proxy.$download.name('用户导入模板.xlsx')
// 2) 按资源路径下载
proxy.$download.resource('/profile/upload/2026/01/01/xxx.png')
// 3) 下载 zip(传入完整相对地址与保存的文件名)
proxy.$download.zip('/common/download/zip?path=xxx', '导出结果.zip')
// 4) 直接保存 Blob
proxy.$download.saveAs(blob, '报表.xlsx')
| 方法 | 说明 |
|---|---|
name(fileName, isDelete = true) | 走 /common/download?fileName=&delete=,取 download-filename 头保存 |
resource(resource) | 走 /common/download/resource?resource= |
zip(url, name) | 下载 zip 包 |
saveAs(blob, name, opts) | 等价于 file-saver 的 saveAs |
printErrMsg(data) | 下载到 JSON 错误体时解析并提示(内部使用) |
需要带业务参数导出 Excel 时,用
request.js的downFile(url, params)/downFilePost(url, data)(见请求封装),它们支持传查询参数与请求体,并自带下载 loading。
