vue-router 2 template

Template for vue-router 2

HTML

<script src="https://npmcdn.com/vue/dist/vue.js"></script>
<script src="https://npmcdn.com/vue-router/dist/vue-router.js"></script>

<div id="app">
  <router-link to="/">/home</router-link>
  <router-link to="/foo">/foo</router-link>
  <router-link to="/bar">/bar</router-link>
  <p>
    To trigger the uncaught promise rejection, click on "/home" and then the button.
  </p>
  <router-view></router-view>
</div>

Babel + JSX

const Home = {
	template: '<div>Home <button type="button" @click="click">$router.push(\'/bar\')</button></div>',
  methods: {
  	click() {
    	// going to /bar via the router-link doesn't trigger the uncaught promise rejection,
      // but $router.push() does
    	this.$router.push('/bar');
    }
  }
}
const Foo = { template: '<div>Foo</div>' }

const router = new VueRouter({
  mode: 'history',
  routes: [
    { path: '/', component: Home },
    { path: '/foo', component: Foo },
    {
    	path: '/bar',
      beforeEnter(to, from, next) {
        // Imagine that /bar redirects to a different page depending on the user type
      	next('/foo');
      },
    },
  ]
})

new Vue({
	router,
  el: '#app',
})