Vue动态修改页面的 title 浏览器页签名称

在 main.js 中动态设置标题

  1. 需求:不同的网址浏览器页签名称要不同

  2. 实现方法:在 main.js 中动态设置标题

javascript 复制代码
// 动态设置页面标题
document.addEventListener('DOMContentLoaded', () => {
  const title = window.location.pathname.includes("/en/") ? 'Support System' : '支持系统';
  document.title = title;
});

使用 document.addEventListener('DOMContentLoaded', ...) 确保 DOM 完全加载后再设置标题。

根据 window.location.pathname 判断当前语言,并设置相应的标题。

main.js完整代码如下:

javascript 复制代码
import Vue from "vue";

import App from "./App.vue";
import store from "./store";
import router from "./router";
import "virtual:svg-icons-register";

import directive from "./directive"; //directive
Vue.use(directive);

//VueMeta
import VueMeta from "vue-meta";
Vue.use(VueMeta);

//Cookie
import Cookies from "js-cookie";

import "@/style.css";
import "animate.css";

import "@support/assets/styles/index.scss"; // global css
import "@support/assets/styles/ruoyi.scss"; // ruoyi css

import "./permission";
import "@/echarts";

// VXETable
import VXETable from "vxe-table";
Vue.use(VXETable);
import VXETablePluginExportXLSX from "vxe-table-plugin-export-xlsx";
VXETable.use(VXETablePluginExportXLSX);
import "vxe-table/lib/style.css";
import "xe-utils";

import Draggable from "vuedraggable";
Vue.component("Draggable", Draggable);

import VueContextMenu from "vue-contextmenu";
Vue.use(VueContextMenu);

import Element from "element-ui";
import VueI18n from "vue-i18n";
import en from "element-ui/lib/locale/lang/en";
import zhCN from "element-ui/lib/locale/lang/zh-CN";
import "@support/assets/styles/element-variables.scss";
Element.Dialog.props.closeOnClickModal.default = false;
Element.Dialog.props.appendToBody.default = true;
Element.TableColumn.props.showOverflowTooltip = {
	type: Boolean,
	default: true
};
Vue.use(VueI18n)
const i18n = new VueI18n({
	locale: window.location.pathname.includes("/en/") ? en : zhCN,
	// locale: en,

	messages: {
		en: { ...en },
		zhCN: { ...zhCN }
	}
});

Element.locale(i18n.locale)

Vue.use(Element, {
	size: Cookies.get("size") || "medium" ,// set element-ui default size
	// i18n: (key, value) => i18n.t(key, value)
});

import { NofeelUI } from "nofeel-ui";
Vue.use(NofeelUI);

Vue.config.productionTip = false;

import "./prototype.js";

import "@support/utils/observe";

// 动态设置页面标题
document.addEventListener("DOMContentLoaded", () => {
	const title = window.location.pathname.includes("/en/") ? "Support System" : "支持系统";
	document.title = title;
})

export default new Vue({
	el: "#app",
	router,
	store,
	// i18n,
	render: (h) => h(App)
});
相关推荐
景尘13 分钟前
vue3 全局注册自定义指令,input聚焦失焦展示对应值
vue.js
CodeCraft Studio22 分钟前
报表控件stimulsoft教程:使用 JoinType 关系参数创建仪表盘
前端·ui
春天姐姐1 小时前
vue知识点总结 依赖注入 动态组件 异步加载
前端·javascript·vue.js
互联网搬砖老肖2 小时前
Web 架构之数据读写分离
前端·架构·web
Pop–3 小时前
Vue3 el-tree:全选时只返回父节点,半选只返回勾选中的节点(省-市区-县-镇-乡-村-街道)
开发语言·javascript·vue.js
滿3 小时前
Vue3 + Element Plus 动态表单实现
javascript·vue.js·elementui
钢铁男儿3 小时前
C# 方法(值参数和引用参数)
java·前端·c#
阿金要当大魔王~~3 小时前
面试问题(连载。。。。)
前端·javascript·vue.js
yuanyxh3 小时前
commonmark.js 源码阅读(一) - Block Parser
开发语言·前端·javascript
进取星辰3 小时前
22、城堡防御工事——React 19 错误边界与监控
开发语言·前端·javascript