vue-router - 같은 URL 강제 리로드

by 1004lucifer

HTML

<script src="https://npmcdn.com/vue-router/dist/vue-router.js"></script>
<div id="app">
  <div>{{$router.currentRoute.fullPath}}</div>
  <router-link to="/home" @click.native="gotoUrl('/home')">/home</router-link>
  <router-link to="/foo" @click.native="gotoUrl('/foo')">/foo</router-link>
  <router-view id="content" :key="$route.fullPath"></router-view>
  <div id="desc">
    같은 URL이면 뒤에 인덱스 값을 붙여서 같은 리소스 URL이더라도 router-view 가 다시 로드된다.
  </div>
</div>

CSS

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}
#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
  height: 250px;
}
#content {
  background: #F8E0E0;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}
#desc {
  background: #E0F8E0;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

Vue

const Home = {
  template: `
  	<div>
    	Home1<br/>
      <span>innerData: {{innerData}}</span><br />
      <button @click="innerData++">증가</button>
    </div>`,
  data () {
    return {
      innerData: 0
    }
  }
}
const Foo = { template: '<div>Foo</div>' }

const router = new VueRouter({
  mode: 'history',
  routes: [
    { path: '/home', component: Home },
    { path: '/foo', component: Foo }
  ]
})

new Vue({
  router,
  el: '#app',
  data: {
    preUrl: '',
    pageIndex: 0
  },
  methods: {
    gotoUrl (toUrl) {
      if (this.preUrl === this.$route.path) {
        this.$router.push(toUrl + '?' + this.pageIndex++)
      }
      this.preUrl = this.$route.path
    }
  }
})