vue项目设置浏览器标题title及图标

一、固定设置标题方案

方法一:在vue.config.js文件,添加如下代码:

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

方法二:直接在public/index.html中修改title即可,如下:

<!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>

二、动态设置标题方案

方法一:通过路由导航守卫设置,使用Vue-Router的beforeEach拦截

/* 第一步:在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
  }
})

方法二:使用插件vue-wechat-title来设置浏览器动态标题

/* 第一步:安装插件 */
npm vue-wechat-title --save

/* 第二步:在全局main.js引入、使用该插件 */
import VueWechatTitle from 'vue-wechat-title' //可以动态修改浏览器标题的插件
Vue.use(VueWechatTitle);

/* 第三步:在router中的index.js路由下设置mate属性 */
const routes = [
	{
    path: '/about',
    name: 'About',
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'),
	meta:{
		title:'关于'
	}
  },
  {
    path: '/test',
    name: 'Test',
    component: () => import(/* webpackChunkName: "test" */ '../views/Test.vue'),
	meta:{
		title:'测试'
	}
  },
]

/* 第四步:我们在APP.vue使用vue-wechat-title插件 */
<router-view v-wechat-title="$route.meta.title"/>

三、图标修改

1、首先做一个ico的小图标,命名为 favicon.ico 放在 /public/下面,替换原有的 favicon.ico,需要可以备份原有的 favicon.ico。
2、在 /public/index.html/里验证,是否引用了 favicon.ico 图标名称。

<!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>

3、重启项目即可。

  • 9
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现 Vue 项目的书签功能,可以考虑使用浏览器提供的 localStorage 来保存用户的书签数据。具体实现步骤如下: 1. 在 Vue 项目中创建一个 Bookmarks 组件,用于显示用户的书签列表和书签添加/删除功能。 2. 在 Bookmarks 组件中,使用 Vue 的生命周期钩子函数 created() 来读取 localStorage 中保存的书签数据。如果 localStorage 中没有保存过书签数据,可以初始化一个空数组。 3. 在 Bookmarks 组件中,使用 v-for 指令循环遍历书签数组,以便在页面中显示所有书签。 4. 在 Bookmarks 组件中,添加一个输入框和添加按钮,用于添加新的书签。当用户点击添加按钮时,将输入框中的书签信息添加到书签数组中,并将数组保存到 localStorage 中。 5. 在 Bookmarks 组件中,为每个书签添加一个删除按钮,用于删除书签。当用户点击删除按钮时,从书签数组中删除对应的书签,并将新的数组保存到 localStorage 中。 6. 在 Bookmarks 组件中,为每个书签添加一个点击事件,用于跳转到对应的页面。可以使用 Vue Router 来实现页面跳转。 下面是一个简单的代码示例: ```html <template> <div> <h2>Bookmarks</h2> <div> <input type="text" v-model="newBookmark"> <button @click="addBookmark">Add</button> </div> <ul> <li v-for="(bookmark, index) in bookmarks" :key="index"> <a :href="bookmark.url" target="_blank">{{ bookmark.title }}</a> <button @click="deleteBookmark(index)">Delete</button> </li> </ul> </div> </template> <script> export default { data() { return { bookmarks: [], newBookmark: '' } }, created() { const savedBookmarks = localStorage.getItem('bookmarks') if (savedBookmarks) { this.bookmarks = JSON.parse(savedBookmarks) } }, methods: { addBookmark() { if (this.newBookmark) { this.bookmarks.push({ title: this.newBookmark, url: 'http://example.com/' + this.newBookmark }) localStorage.setItem('bookmarks', JSON.stringify(this.bookmarks)) this.newBookmark = '' } }, deleteBookmark(index) { this.bookmarks.splice(index, 1) localStorage.setItem('bookmarks', JSON.stringify(this.bookmarks)) } } } </script> ``` 注意,这只是一个简单的示例,实际项目中可能需要更复杂的实现。例如,可以添加更多的书签信息,如书签描述、图标等。同时,为了更好的用户体验,可以使用第三方库来美化书签列表和添加/删除操作的界面。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值