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)
});
相关推荐
会编程的土豆3 分钟前
前端和后端是怎么配合工作的(Go后端视角)
前端·golang·状态模式
w_t_y_y5 分钟前
vue父子组件通信(一)父子调用和通信(2)VUE3
前端·javascript·vue.js
Demon1_Coder8 分钟前
Day1-SpringAI-1.0.0版本
java·开发语言·前端
ZC跨境爬虫9 分钟前
跟着 MDN 学CSS day_42:等分轨道、层叠放置与混合布局
前端·javascript·css·ui·html
Cheney95019 分钟前
Vue 项目字体文件打包后 fonts 文件夹“消失”?原因分析与解决方案
前端·javascript·vue.js
问心无愧051316 分钟前
ctf show web入门68,69
android·前端·笔记
jingling55518 分钟前
Flutter | 从基本跳转到路由守卫
服务器·前端·网络·flutter·前端框架
神奇的代码在哪里24 分钟前
【单机离线版】大学考试题库复习工具:前端离线Excel解析 + localStorage持久化 + Playwright
前端·html·ai编程·题库复习·刷题软件·大学考试
daols8828 分钟前
vxe-table 实现数据分组统计与表尾合计
前端·javascript·vue.js·vxe-table
向日的葵00630 分钟前
Vue 函数定义、事件绑定与列表渲染精讲
前端·javascript·vue.js