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