vue3.0设置浏览器icon和动态修改页面title

5 篇文章 0 订阅

在这里插入图片描述

设置浏览器icon

启用了pwa:
参考文章:vue3.0设置浏览器icon

文件结构
在这里插入图片描述
vue.config.js中设置:
注:vue.config.js是vue-cli的配置文件,可选,可参考:全局-cli-配置

pwa: {
    iconPaths: {
      favicon32: './favicon.ico',
      favicon16: './favicon.ico',
      appleTouchIcon: './favicon.ico',
      maskIcon: './favicon.ico',
      msTileImage: './favicon.ico'
    }
  },

动态设置页面title

参考文章:
聊聊 Vue 中 title 的动态修改

方法一:router改document.title

在router.js中写

const defaultTitle = 'home'; 
router.beforeEach((to, from, next) => {
  document.title = to.meta.title ? to.meta.title : defaultTitle;
  next();
});

方法二:使用vue-meta插件

可改多个页面meta,有利于SEO,实现较为优雅
参考文章:vuex结合vue-meta实现router动态设置meta标签
使用:
先install插件,
npm install vue-meta
MAC中好像需要 sudo cnpm install vue-meta
(npm 和 cnpm 都行啦,看自己的网速快慢)

在main.js中

import Meta from 'vue-meta';
Vue.use(Meta);

new Vue({
  el: '#app',
  router,
  metaInfo () {
    return {
      title: 'home页',  // 在这里直接用了同一个title,如果每个页面的title不一样,参考上述链接中的做法
    };
  },
  render: h => h(app)
});

其他问题:

刷新时仍然会显示Vue App

在这里插入图片描述
在这里插入图片描述
这是由于配置文件vue.config.js中配置有问题,默认重新生成一个index.html
我加了pages项,默认以自定义的index.html为模板
参考文章:如何配置 vue-cli 3.0 的 vue.config.js

 pages: {
    index: {
      // entry for the pages
      entry: 'src/main.js',
      // the source template
      template: 'src/index.html',
      // output as dist/index.html
      filename: 'index.html',
      // when using title option,
      // template title tag needs to be <title><%= htmlWebpackPlugin.options.title %></title>
      title: '首页',
      // chunks to include on this pages, by default includes
      // extracted common chunks and vendor chunks.
      chunks: ['chunk-vendors', 'chunk-common', 'index']
    }
    // when using the entry-only string format,
    // template is inferred to be `public/subpage.html`
    // and falls back to `public/index.html` if not found.
    // Output filename is inferred to be `subpage.html`.
    // subpage: ''
  },

文档结构:
在这里插入图片描述

最上面说的动态修改页面title是指,比如我打开了一篇文章,页面的title相应的变成那篇文章的题目;打开另一篇是另一篇的题目,like this:
在这里插入图片描述

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值