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

Implementation of TypeScript in React project

Table of contents 1. Introduction 2. Usage Statel...

Practical experience of implementing nginx to forward requests based on URL

Preface Because this is a distributed file system...

Solution to the problem that Docker container cannot access Jupyter

In this project, the Docker container is used to ...

JavaScript String Object Methods

Table of contents Methods of String Object Method...

N ways to achieve two-column layout with CSS

1. What is a two-column layout? There are two typ...

Docker deployment of Kafka and Spring Kafka implementation

This article mainly introduces the deployment of ...

Detailed explanation of the TARGET attribute of the HTML hyperlink tag A

The hyperlink <a> tag represents a link poin...

Introduction to the B-Tree Insertion Process

In the previous article https://www.jb51.net/arti...

10 bad habits to avoid in Docker container applications

There is no doubt that containers have become an ...

Conditional comments to determine the browser (IE series)

<!--[if IE 6]> Only IE6 can recognize <![...

Detailed troubleshooting of docker.service startup errors

Execute the following command to report an error ...

Solution to the automatic stop of MySQL service

This article mainly introduces the solution to th...