1. 程式人生 > >vue-router在IE11中頁面不跳轉

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], });