cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div id="app">
  <h1>Vue Router Navigation Guards Explained</h1>


  <nav class="mainNav">
    <!-- use router-link component for navigation. -->
    <router-link :to="{ name: 'home'}">Home</router-link>
    <!-- specify the link by passing the `to` prop. -->
    <router-link :to="{ name: 'user', params: { userId: 123, userName: 'Test User 1' } }">User 123</router-link>
    <router-link :to="{ name: 'user', params: { userId: 456, userName: 'Test User 2' } }">User 456</router-link>
    <router-link :to="{ name: 'user', params: { userId: 789, userName: 'Test User 3' } }">User 789</router-link>
    <router-link to="/bar">Bar</router-link>
    <router-link to="/foo">Foo</router-link>
    <router-link to="/error">Error</router-link>
  </nav>
  <!-- route outlet -->
  <!-- component matched by the route will render here -->
  <section class="mainBody">
    <router-view></router-view>
  </section>
  
</div>
            
          
!
            
              #app {
  max-width: 800px;
  margin: auto;
}

code {
  background: #f2f2f2;
  padding: 0 0.5rem;
}

.mainBody {
  border-top: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  margin: 1rem 0;
  padding: 1rem 0;
}

nav.mainNav {
  > * {
    padding: 0 0.75rem;
    text-decoration: none;
  }
  
  > *:nth-last-child(n+2) {
    border-right: 1px solid #aaa;
  }
}
            
          
!
            
              const Home = {
  template: `
    <section>This is the home screen.</section>
  `
} 

const Foo = {
  template: `
    <section>You will not see this template when Foo is redirected to Home.</section>
  `
} 

const Err = {
  template: `
    <section>
      You will not see this template. The error is passed to our error handler.
    </section>
  `
} 

const Bar = { 
  template: `
    <section>
      <div>Bar</div>
    </section>
  `,
  // set guard on the component options object:
  beforeRouteEnter ( to, from, next ) {
    console.log('Entering Bar')
    
    // Pass a callback to next (optional)
    next(vm => {
      // this callback has access to component instance (ie: 'this') via `vm`
      vm.testFunc('Some Message')
      console.log("Fully Entered Bar")
    })
  },
  // set guard on the component options object:
  beforeRouteLeave ( to, from, next ) {
    console.log('Leaving Bar')
    next()
  },
  methods: {
    testFunc(msg) {
      console.log(msg)
    }
  }
}

const User = {
  props: ["userId", "userName"],
  template: `
    <div>
      User - {{userName}}({{userId}})
    </div>
  `,
  // set guard on the component options object:
  beforeRouteLeave ( to, from, next ) {
    console.log('Leaving User')
    next()
  },
  /* Called when this component is reused.
  Here we have a chance to update the component
  since mounted() and other life-cycle hooks won't be called 
  when a component is reused. */
  beforeRouteUpdate ( to, from , next ) {
    console.log('Reusing this component.')
    next()
  }
}

const routes = [
  { path: '/', name: 'home', component: Home},
  { path: "/bar", name: 'bar', component: Bar },
  { path: "/foo", name: 'foo', component: Foo },
  { path: "/error", name: 'error', component: Err },
  {
    path: "/user/:userId",
    name: "user",
    component: User,
    props: true,
    // set nav guard on the route definition object:
    beforeEnter: (to, from, next) => {
      console.log('Entering User')

      next()
    }
  }
  
]

const router = new VueRouter({
  routes // short for routes: routes
})

// GLobal BEFORE hooks:
router.beforeEach((to, from, next) => {
  
  // re-route
  if (to.path === '/foo') {
    next('/')
  } 
  // Abort navigation based on some criteria:
  else if (to.path === '/user/123') {
    next(false)
  } 
  else if (to.path === '/error') {
    const err = new Error('My Error Message')
    
    // pass the error to onError() callback.
    next(err)
  }
  else {
    next()
  }
})

// GLobal AFTER hooks:
router.afterEach((to, from) => {
  // This fires after each route is entered.
  console.log(`Just moved from '${from.path}' to '${to.path}'`)
})

// Register an Error Handler:
router.onError(err => {
  console.log('Handling this error', err)
})

const app = new Vue({
  router
}).$mount("#app")

            
          
!
999px
Loading ..................

Console