動態設置html的title


使用vue前端框架做,竟然丟棄了很多javascript和html的東西了。。
動態設置title的方法:

1.使用vue的自定義指令


<div v-title>{{htmltitle}}</div>
...
directives: {
  title: {
      inserted: function (el, binding) {
          document.title = el.innerText
          el.remove()
      }
  }
}

2.很簡單


// 設置html title
document.title = sessionStorage.getItem('title')

3.router路由不同的title


...
routes: [
        {
            path: '/index',
            name: 'index',
            component: index,
            meta:{
                title:'首頁'
            }
        },
        {
            path: '/user',
            name: 'user',
            component: user,
            meta:{
                title:'個人中心'
            }
        }
    ]
...
router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        document.title = to.meta.title
    }
    next()
})

原文地址:https://segmentfault.com/a/1190000016801359


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM