25.06.2018 Views

Full-Stack Vue.js 2 and Laravel 5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

next<br />

An important feature of navigation guards is that they will halt navigation until<br />

the next function is called. This allows asynchronous code to be executed before<br />

the navigation is resolved: beforeRouteEnter(to, from, next) { new<br />

Promise(...).then(() => { next(); }); }<br />

You can pass false to the next function to prevent a navigation, or you can pass a<br />

different route to redirect it. If you don't pass anything, the navigation is<br />

considered confirmed.<br />

The beforeRouteEnter guard is a special case. Firstly, this is undefined within it<br />

since it is called before the next page component has been created:<br />

beforeRouteEnter(to, from, next) { console.log(this); // undefined }<br />

However, the next function in beforeRouteEnter can accept a callback function as an<br />

argument, for example, next(component => { ... }); where component is the page<br />

component instance.<br />

This callback is not triggered until the route is confirmed <strong>and</strong> the component<br />

instance has been created. Due to how JavaScript closures work, the callback<br />

will have access to the scope of the surrounding code where it was called:<br />

beforeRouteEnter(to, from, next) { var data = { ... } next(component => {<br />

component.$data = data; }); }

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!