Notice: 函数 WP_Scripts::localize 的调用方法不正确$l10n 参数必须是一个数组。若要将任意数据传递给脚本,请改用 wp_add_inline_script() 函数。 请查阅调试 WordPress来获取更多信息。 (这个消息是在 5.7.0 版本添加的。) in /data/www/appblog/wp-includes/functions.php on line 6131

Vue刷新当前页面或组件

利用v-if控制router-view,在根组件APP.vue中实现一个刷新方法,然后通过provide/inject方式调用。

1、修改APP.vue,通过声明reload方法,控制router-view的显示或隐藏,从而控制页面的再次加载,这边定义了isRouterAlive来控制

<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>

2、在需要刷新的页面中注入(inject)在App.vue组件提供(provide)reload依赖,然后直接使用this.reload()调用即可

export default {
  inject: ['reload'], // 注入依赖
  name: 'country',
  data () {
    countryCode: '',
    countryList: []
  },
  methods: {
    changeCountry () {
      switch (this.countryCode) {
        case 'other':
          $('#country_name')[0].disabled = false
          break
        default:
          $('#country_name')[0].disabled = true
          this.reload() // 刷新页面
          break
      }
    }
  }
}
</script>
上一篇 Vue踩坑:Html属性差值弃用双大括号改用v-bind
下一篇 Vue全局Filter体验