vue-router在IE11中頁面不跳轉


情景:

IE11瀏覽器中,在進行正常頁面跳轉操作后(頁面A跳轉到頁面B),點擊瀏覽器的左上角的‘后退’按鈕,點擊后,可以看到url地址已經發生了變化(url由頁面B變為頁面A),hash值也已經是上一頁的路由,但是瀏覽器顯示的內容卻沒有發生變化(依舊是頁面B)。若將url在一個新的選項卡中復制粘貼是可以打開頁面的(頁面A用當前url打開沒問題)。

沒有任何報錯(頁面A和頁面B無任何js錯誤或者兼容性錯誤)。

若有錯誤也會導致頁面跳轉不成功,頁面依舊是當前頁面,但是控制台會報ERROR。

解決方法:

IE11上router-link無法跳轉,主要是因為當url的hash change的時候,瀏覽器沒有做出相應。這時候需要做一個兼容,當瀏覽器是IE11時手動給url加一個hashChange事件。

方案一:

new Vue({
  el: '#app',
  router,
  store,
  template: '<Layout/>',
  components: { Layout },
  render: function (createElement) {
    if ('-ms-scroll-limit' in document.documentElement.style && '-ms-ime-align' in document.documentElement.style) {
      window.addEventListener('hashchange', () => {
        var currentPath = window.location.hash.slice(1)
        if (this.$route.path !== currentPath) {
          this.$router.push(currentPath)
        }
      }, false)
    }
    return createElement(Layout);
  }
})

方案二:

const IE11RouterFix = {
    methods: {
        hashChangeHandler: function() { this.$router.push(window.location.hash.substring(1, window.location.hash.length)); },
        isIE11: function() { return !!window.MSInputMethodContext && !!document.documentMode; }
    },
    mounted: function() { if ( this.isIE11() ) { window.addEventListener('hashchange', this.hashChangeHandler); } },
    destroyed: function() { if ( this.isIE11() ) { window.removeEventListener('hashchange', this.hashChangeHandler); } }
};

new Vue({
    /* your stuff */
    mixins: [IE11RouterFix],
});

 


免責聲明!

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



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