Home > Web Front-end > Vue.js > Teach you two methods to set Vue dynamic browser title (detailed examples)

Teach you two methods to set Vue dynamic browser title (detailed examples)

WBOY
Release: 2021-12-24 18:31:37
forward
2965 people have browsed it

This article brings you how to use vue to achieve the effect of dynamically setting the browser title. I hope it will be helpful to everyone.

Teach you two methods to set Vue dynamic browser title (detailed examples)

vue dynamically sets the browser title

Usually setting the browser title is as shown below:

Teach you two methods to set Vue dynamic browser title (detailed examples)

But Vue is a single-page application, and the entry file only has one HTML, and only one label can be set, so the following is an introduction to two commonly used methods of dynamically setting browser labels

Text

The first one

Use the browser’s native method document.title

router/index.js
In router.beforeEach

//多语言项目,根根据自己项目来
 import i18n from '@/i18n/index';
 document.title = i18n.t("router." + to.name)
 
 //单语言项目
  document.title = to.name
Copy after login

Teach you two methods to set Vue dynamic browser title (detailed examples)
The language switching route remains unchanged, so you need to add it. Single language projects do not need it

//多语言项目
  document.title = i18n.t("router." + to.name)
Copy after login

Teach you two methods to set Vue dynamic browser title (detailed examples)
Complete, recommended, good native compatibility, no need to download and install other dependent packages

Second option

Use plug-ins

1. Install plug-ins

npm install vue-wechat-title --save
Copy after login

2.main.js reference

import VueWechatTitle from 'vue-wechat-title'//动态修改title
Vue.use(VueWechatTitle)
Copy after login

3.Add instructions

//多语言项目
  <router-view></router-view>
 //单语言项目
  <router-view></router-view>
Copy after login

Complete

Notes

Note: The value is based on Come from your own project routing structure, this demo uses the name value, i18n has a corresponding language package,
You can add a title attribute to the meta object, and use to.meta.title outside
Teach you two methods to set Vue dynamic browser title (detailed examples)
Teach you two methods to set Vue dynamic browser title (detailed examples)

[Related recommendations: "vue.js tutorial"]

The above is the detailed content of Teach you two methods to set Vue dynamic browser title (detailed examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
vue
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template