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.

Route navigation guards<br />

Similar to lifecycle hooks, navigation guards allow you to intercept <strong>Vue</strong> Router<br />

navigations at a particular point in their life cycle. These guards can be applied<br />

to a specific component, a specific route, or to all routes.<br />

For example, afterEach is the navigation guard called after any route is navigated<br />

away from. You might use this hook to store analytics information, for example:<br />

router.afterEach((to, from) => { storeAnalytics(userId, from.path); })<br />

We can use the beforeRouteEnter navigation guard to fetch data from our web<br />

service if the data in the head is unsuitable. Consider the following pseudo-code<br />

for how we might implement this:<br />

beforeRouteEnter(to, from, next) {<br />

if (to !== injectedData.path) {<br />

getDataWithAjax.then(data => {<br />

applyData(data)<br />

})<br />

} else {<br />

applyData(injectedData)<br />

}<br />

next()<br />

}

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

Saved successfully!

Ooh no, something went wrong!