美文网首页
微信浏览器设置网页标题

微信浏览器设置网页标题

作者: 洋洋_13dc | 来源:发表于2019-06-12 14:27 被阅读0次

    一般设置网页标题只需要document.title = ‘首页’就可以

    但是奈何微信浏览器比较特殊

    只渲染一次便不再更新title

    所以可以先创建一个iframe

    然后再删除

    亲测有效

    setTitle(t) {

          document.title = t

          var i = document.createElement('iframe')

          // i.src = '//m.baidu.com/favicon.ico'

          i.style.display = 'none'

          i.onload = function() {

            setTimeout(function() {

              i.remove()

            }, 9)

          }

          document.body.appendChild(i)

        }

    方法调用setTitle(‘首页’)


    在vue中,不同的页面设置不同的标题

    可以在路由中设置

    路由设置meta(scrollTop是在路由跳转时默认回到最上面位置,title就是网页标题)

    然后在路由跳转监测时(在app.vue)

    watch: {

        '$route' (to, from) {

          this.documentTitle = to.meta.title ? to.meta.title : ''

          document.title = this.documentTitle

          // web-view下的页面内

          console.log('isMiniprogram', window.__wxjs_environment === 'miniprogram')

          // window.webkit.messageHandlers.WXPay.postMessage({body: 'abc'})

          this.toPath = to.path

          this.fromPath = from.path

          // 页面需要滚动到顶部

          if (to.matched.some(m => m.meta.scrollToTop)) {

            setTimeout(_ => {

              document.body.scrollTop = 0

            }, 300)

          }

    }

    完美

    相关文章

      网友评论

          本文标题:微信浏览器设置网页标题

          本文链接:https://www.haomeiwen.com/subject/irhlfctx.html