微信web开发问题记录

问题1、微信浏览器中没法使用reload重载文档【VUE框架】

问题分析:vue

  • 微信不支持location.reload()方法,在微信浏览器中会失效
  • Vue中的路由跳转是相似于ajax局部刷新,所以使用location.href=‘xxx+时间戳’ 这种方法时,页面不会重载
  • Vue自带的this.$router.go(0)无效
  • history.go(0)无效

vue框架开发解决:ajax

在App.vue中,先在provide中注册一个用于页面重载的方法浏览器

<template>
  <div class="app-wrapper">
    <router-view v-if="isRouterAlive" />
  </div>
</template>

<script>
export default {
  name: 'app',
  provide () {
    return {
      appReload: this.reload // 经过provider来提供变量
    }
  },
  data () {
    return {
      isRouterAlive: true // 解决微信端页面刷新问题
    }
  },
  methods: {
    // 实现重载
    reload () {
      this.isRouterAlive = false
      this.$nextTick(() => {
        this.isRouterAlive = true
      })
    }
  }
}
</script>

而后在子组件中经过inject来注入变量,直接调用reload就能够了微信

<template>
  <div>
     <button @click="handlerReload">刷新</button>
  </div>
</template>

<script>
  export default {
    inject: ['appReload'], // 经过inject来注入变量
    methods: {
        handlerReload () {
            this.appReload()
        }
    }
  }
</script>

扩展知识:app

provider/inject:简单的来讲就是在父组件中经过provider来提供变量,而后在子组件中经过inject来注入变量。
这对选项须要一块儿使用,以容许一个祖先组件向其全部子孙后代注入一个依赖,不论组件层次有多深,并在起上下游关系成立的时间里始终生效。

参考资料:https://blog.csdn.net/Lucky_Q/article/details/89097423框架

相关文章
相关标签/搜索