Vue project set browser title title and icon

1. Change the title of the browser in the vue project

Method 1: In the vue.config.js file, add the following code:

chainWebpack: config => {
        config.plugin('html')
            .tap(args => {
                args[0].title = '标题';
                return args;
        )
}

Method 2: Just modify the title directly in public/index.html, as follows:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <title>要修改显示的标题</title>
  </head>
  <body>
  </body>
</html>

2. Change the icon of the browser in the vue project

1. First make a small ico icon, name it favicon.ico and place it under /public/ to replace the original favicon.ico. You need to back up the original favicon.ico.

2. Verify in /public/index.html/ whether the favicon.ico icon name is referenced.

3. Dynamically control the title of the browser in the project

Guard settings through route navigation , using Vue-Router's beforeEach interception

/* 第一步:在router中的index.js路由下设置mate属性*/ 
routes: [{
      path: '/',
      name: 'home',
      component: () => import('@/pages/home/index'),
      meta:{
        keepAlive: true
      }
    },
    {
      path: '/person/auth,
      name: 'personAuth',
      component: () => import('@/pages/person/auth),
      meta:{
        title: '功能授权',
        keepAlive: false
      }
    }
  ]
 
/* 第二步:在路由守卫router.beforeEach中设置如下代码 */
router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
})

Guess you like

Origin blog.csdn.net/lovecoding1/article/details/128718940