8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vue.js ページ全体更新されず、コンポーネントのみ更新(reload)の方法

Last updated at Posted at 2019-11-01

背景

vueは<script> タグを直接組み込み!
すべてのコードを一つhtmlに書く!
ページ全体更新したくない!
特定のコンポーネントのみ更新したい!
という場合は、

[provide/inject]を使いましょう!

実装

実装ロジック:

reload()でisRouterShow(true/false)をコントロール!
これにより、router-viewの表示/非表示をコントロール!
さらに、これにより、コンポーネントの更新(reload)をコントロール!

1、親コンポーネントでproviderを作成

router-viewの表示/非表示をコントロール.html
<body>
  <div id="app">
       <router-view v-if='isRouterShow' class="reload"></router-view>  <!-- これ!router-viewの表示/非表示をコントロール -->
  </div>
</body>

reload().
 var vm = new Vue({
    router:router,
    el: '#app'
    provide() {
      return {
        reload: this.reload 
      }
    },
    data() {
      return {
        isRouterShow: true
      }
    },
    methods: {
      async reload() {                    //<-これ!isRouterShow(true/false)をコントロール!
        this.isRouterShow = false
        await this.$nextTick()
        this.isRouterShow = true
      }
    }
  })

2、子コンポーネントにinjectでreload()を導入! reloadしたい場所に this.reload(); で書けばいい!

child-reload.
 Vue.component('childreload', {
   inject: ['reload'],    // <- これ!injectによりこのコンポーネントの更新をコントロールできるようになる
    data:function() {
      return {
        dateValue: null
      }
    },
    methods: {
      abtn: function() {
        this.bus.$emit("passData", this.dateValue) ;
        this.reload();    //例えばここ
      }

    },
    template: '#XXXXXXX'
  })

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?