Home > Web Front-end > JS Tutorial > body text

How to set the title method of each page using vue-router

亚连
Release: 2018-06-06 11:00:40
Original
2992 people have browsed it

Below I will share with you an article using vue-router to set the title of each page. It has a good reference value and I hope it will be helpful to everyone.

Basic environment configuration: webpack vue2.0 vue-router nodeJS

Enter the index.js file under the router folder

First introduce:

import Vue from 'vue'
import Router from 'vue-router'
Copy after login

Then configure the address of each route in the routing:

routes: [
 {   /* (首页)默认路由地址 */
  path: '/',
  name: 'Entrance',
  component: Entrance,
  meta: {
  title: '首页入口'
  }
 },
 {   /* 修改昵称 */
  path: '/modifyName/:nickName',
  name: 'modifyName',
  component: modifyName,
  meta: {
  title: '修改昵称'
  }
 },
 {   /* 商品详情 */
  path: '/goodsDetail',
  name: 'goodsDetail',
  component: goodsDetail,
  meta: {
  title: '商品详情'
  }
 },
 { /* Not Found 路由,必须是最后一个路由 */
  path: '*',
  component: NotFound,
  meta: {
  title: '找不到页面'
  }
 }
 ]
Copy after login

Set the title name of the page in each meta, and finally traverse

router.beforeEach((to, from, next) => {
 /* 路由发生变化修改页面title */
 if (to.meta.title) {
 document.title = to.meta.title
 }
 next()
})
Copy after login

This adds a title to each VUE page

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Use Vue.set to dynamically add object attributes in Vue

There is no dev in vue2.0 -Configuration method under server.js

How to implement the file dragging and uploading function with progress bar in Vue

The above is the detailed content of How to set the title method of each page using vue-router. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!