Detailed explanation of how to dynamically set the browser title in Vue

Detailed explanation of how to dynamically set the browser title in Vue

nonsense

Usually, the browser title is set like this

But Vue is a single-page application, and the entry file only has one HTML, so only one tag can be set. So here are two commonly used methods to dynamically set browser tags.

text

The first

Use the browser native method document.title

router/index.js

router.beforeEach

//For multi-language projects, import i18n from '@/i18n/index' according to your own project;
 document.title = i18n.t("router." + to.name)
 //Single language project document.title = to.name


The language switching route remains unchanged, so it should be added as well. Single language projects do not need it.

//Multi-language project document.title = i18n.t("router." + to.name)


Completed, recommended, native compatibility is good, no need to download and install other dependent packages

The second

Using plugins

1. Install the plugin

npm install vue-wechat-title --save

2.main.js reference

import VueWechatTitle from 'vue-wechat-title'//Dynamically modify title
Vue.use(VueWechatTitle)

3. Add instructions

//Multi-language project <router-view v-wechat-title="$t('router.' + $route.name)" ></router-view>
 //Single language project <router-view v-wechat-title=" $route.name" ></router-view>

Finished

notes

Note: The value depends on the routing structure of your own project. This demo uses the name value. i18n has a corresponding language package.

You can add a title attribute in the meta object and use to.meta.title outside of it.


Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • javascript modifies the browser title method JS dynamically modifies the browser title
  • How to dynamically change the browser title using JS
  • How to display the current date and time in the browser title bar using JavaScript
  • Tips for modifying browser tab title with JavaScript

<<:  Introduction to the usage of exists and except in SQL Server

>>:  The difference between float and position attributes in CSS layout

Recommend

Sample code for cool breathing effect using CSS3+JavaScript

A simple cool effect achieved with CSS3 animation...

How to build a private Docker repository using Harbor

Table of contents 1. Open source warehouse manage...

How to install multiple mysql5.7.19 (tar.gz) files under Linux

For the beginner's first installation of MySQ...

Native js to realize the upload picture control

This article example shares the specific code of ...

Use CSS to draw a file upload pattern

As shown below, if it were you, how would you ach...

Detailed explanation of selinux basic configuration tutorial in Linux

selinux ( Security-Enhanced Linux) is a Linux ker...

Solution to the problem that the mysql8.0.11 client cannot log in

This article shares with you the solution to the ...

Implementation process of nginx high availability cluster

This article mainly introduces the implementation...

Tutorial on installing mysql5.7.18 on mac os10.12

I searched the entire web and found all kinds of ...

MySQL index usage monitoring skills (worth collecting!)

Overview In a relational database, an index is a ...

How to open external network access rights for mysql

As shown below: Mainly execute authorization comm...

CSS and HTML and front-end technology layer diagram

The relationship between Javascript and DOM is ve...

Detailed implementation plan of Vue front-end exporting Excel files

Table of contents 1. Technology Selection 2. Tech...

Practice of using SuperMap in Vue

Table of contents Preface Related Materials Vue p...