25.06.2018 Views

Full-Stack Vue.js 2 and Laravel 5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using the CDN in <strong>Vue</strong><br />

In our <strong>Vue</strong> app, we're loading some static assets as well. For example, in the<br />

toolbar we use the logo.<br />

resources/assets/components/App.vue: <br />

As this is a relative URL it will, by default, point to the web server. If we make it<br />

an absolute URL instead, we'd have to hard-code the CDN URL, which is not<br />

ideal either.<br />

Let's instead get <strong>Laravel</strong> to pass the CDN URL in the head of the document. We<br />

can do this by simply calling the cdn helper with an empty string.<br />

resources/views/app.blade.php: ... ...<br />

window.cdn_url = "{{ cdn('') }}";<br />

<br />

<br />

We'll now use a computed property to construct the absolute URL using this<br />

global value.<br />

resources/assets/components/App.vue: ... vuebnb ...<br />

export default { computed: { logoUrl() { return<br />

`${window.cdn_url || ''}images/logo.png`; } }, ... } ...<br />

We'll use the same concept in the footer where the grey logo is used.<br />

resources/assets/components/CustomFooter.vue: ... ... export default { computed: {<br />

containerClass() { ... }, logoUrl() { return `${window.cdn_url ||<br />

''}images/logo_grey.png`; } }, }

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

Saved successfully!

Ooh no, something went wrong!