<address id="ttjl9"></address>

      <noframes id="ttjl9"><address id="ttjl9"><nobr id="ttjl9"></nobr></address>
      <form id="ttjl9"></form>
        <em id="ttjl9"><span id="ttjl9"></span></em>
        <address id="ttjl9"></address>

          <noframes id="ttjl9"><form id="ttjl9"></form>

          vue項目刷新當前頁面的幾種方式

          2019-11-4    seo達人

          在vue項目中,經常會遇到需要刷新當前頁面的需求。

          因為vue-router判斷如果路由沒有變化,是不會刷新頁面獲取數據的。



          方式1:go(0)和reload()

          通過location.reload()或是this.$router.go(0)兩種強制刷新方式,相當于按F5,會出現瞬間白屏,體驗差,不推薦。



          方式2:定義一個空白路由頁面,路由跳轉到該空白頁后立馬跳回當前頁,實現路由刷新。

          在router路由表中定義一個空白路由,



           // 強制刷新當前頁所用的中間跳轉頁

             {

                  path: '/redirect/:path*',

                  component: () => import('@/views/redirect/index')

            }



          寫一個空白路由組件



          //redirect/index

          <script>

          export default {

            created() {

              const { params, query } = this.$route

              const { path } = params

              this.$router.replace({ path: '/' + path, query })

            },

            render: function(h) {

              return h() // avoid warning message

            }

          }

          </script>





          在需要刷新的頁面使用



          refresh() {

                // 刷新當前路由

                const { fullPath } = this.$route

                this.$router.replace({

                  path: '/redirect' + fullPath

                })

              }



          這種方式,基本上能夠應付絕大多數情況,推薦使用。

          但是,有時候,有一些極端情況下,這種刷新不起作用,而又不想用第一種那種毛子般的簡單粗暴的方式的話,下面的方式可以選擇使用。



          方式3:provede/inject 方式

          vue官方文檔說了,這個依賴注入方式是給插件開發使用的,普通應用中不推薦使用。

          但是,效果卻很好。

          app.vue修改



          <template>

            <div id="app">

              <router-view v-if="isRouterAlive" />

            </div>

          </template>

          <script>

          export default {

            name: 'App',

            provide() {

              return {

                reload: this.reload

              }

            },

            data() {

              return {

                isRouterAlive: true

              }

            },

            methods: {

              reload() {

                this.isRouterAlive = false

                this.$nextTick(function(){

                  this.isRouterAlive = true

                })

              }

            }

          }

          </script>





          使用的時候:

          demo.vue



          <template>

            <div class="container">

            xxx

            </div>

          </template>



          <script>

          export default {

            inject: ['reload], // 依賴注入

            name: 'Demo',

            computed: {

              message() {

                return '抱歉,您訪問的頁面地址有誤或者該頁面不存在...'

              }

            },

            methods: {

            handleReload() {

            this.reload() // 直接在需要刷新的方法中調用這個reload()

          }

            }

          }

          </script>



          <style lang="scss" scoped>

          </style>



          原理就是通過依賴注入的方式,在頂部app通過v-if的顯示隱藏來強制切換顯示,以此來讓vue重新渲染整個頁面,app中通過provide方式定義的reload方法,在它的后代組件中,無論嵌套多深,都能夠觸發調用這個方法。具體說明查看官方文檔。


          日歷

          鏈接

          個人資料

          藍藍設計的小編 http://www.syprn.cn

          存檔

          亚洲va欧美va天堂v国产综合