vue router 修改title(IOS 下動態改變title失效)


在ios下app  設置document.title = "titleName" 失效,原因是在IOS webview中網頁標題只加載一次,動態改變是無效的。

在路由配置中添加  meta對象 如:

 

在路由配置js里面添以下代碼

router.afterEach(route => {
    // 從路由的元信息中獲取 title 屬性
    if (route.meta.title) {
        document.title = route.meta.title;
        // 如果是 iOS 設備,則使用如下 hack 的寫法實現頁面標題的更新
        if (navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
            const hackIframe = document.createElement('iframe');
            hackIframe.style.display = 'none';
            hackIframe.src = '/static/html/fixIosTitle.html?r=' + Math.random();
            document.body.appendChild(hackIframe);
            setTimeout(_ => {
                document.body.removeChild(hackIframe)
            }, 300)
        }
    }
});

 在static下添加一個空頁面

 

完美解決問題;

摘抄自 :https://segmentfault.com/a/1190000008853962


免責聲明!

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



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