http://bit.ly/ijs-nuxt
Web Chapter Lead at
SHARE NOW (formerly car2go)
Get in touch! david.guijarro@share-now.com twitter.com/davguij
Content
Database, API...
Middleware
Transform
Cache
Nuxt.js
Browser
Android
iOS
Super cool device
Page === route
/pages/*.vue ➠ 🧙♂️ ➠ routes
/pages/**/*.vue ➠ 🧙♂️ ➠ nested routes
_id.vue ➠ 🧙♂️ ➠ this.$route.params.id
<nuxt-link to="/somewhere">Go somewhere</nuxt-link>Codeless link prefetching!
Grab dynamic path parameters with
this.$route.params
// returns object with parametersUse them directly in your markup
<h3>Selected language: {{$route.params.language}}</h3>Use the asyncData lifecycle method and the path parameters to decide what to render on page initialization
const languages = new Map();
languages.set('english', 'Hello, iJS London 2019! So glad to be here!');
export default {
asyncData({params}) {
return {translatedGreeting: languages.get(params.language)};
}
}
nuxt.config.js
export default {
// ...
head: {
titleTemplate: 'This is the global title',
meta: [{
hid: 'description',
name: 'description',
content: 'This is the global description'
}]
}
// ...
}export default {
head() {
return {
title: 'Title for this page only',
meta: [{
hid: 'description',
name: 'description',
content: 'Description for this page only'
}]
};
}
}Component override
Hydration refers to the client-side process during which the static HTML sent by the server turns it into dynamic DOM that can react to client-side data changes.
Static HTML ➠ Interactive SPA
<no-ssr placeholder="Loading...">
<!-- this component will only be rendered on client-side -->
<awesome-component />
</no-ssr>// nuxt.config.js
export default {
plugins: [
{ src: '~/plugins/both-sides.js' },
{ src: '~/plugins/client-only.js', mode: 'client' },
{ src: '~/plugins/server-only.js', mode: 'server' }
]
}
export default {
methods: {
clientOnlyMethod() {
if (process.client) {
window.something();
}
return;
}
}
}npm i vue-lazy-hydration⚠️ index.js is mandatory!
⚠️ "this" cannot be used inside neither "asyncData()" nor "fetch()" because the component is not yet instantiated
export const actions = {
async nuxtServerInit({ dispatch }) {
await dispatch('config/fetch');
}
};
// nuxt.config.js
module.exports = {
modules: ['@nuxtjs/axios', '@nuxtjs/proxy'],
proxy: {
'/api': {
target: 'http://example.com',
pathRewrite: {
'^/api': '/',
},
},
},
};
default.vue
<template>
<div>
<header>
<h1>Welcome</h1>
</header>
<main>
<nuxt/>
</main>
</div>
</template>
with-header.vue
<template>
<nuxt />
</template>with-sidebar.vue
<template>
<div>
<main>
<nuxt/>
</main>
<aside>
<my-sidebar />
</aside>
</div>
</template>
one-page.vue
export default {
layout: 'with-header'
// ...
}
export default {
layout: 'with-sidebar'
// ...
}
another-page.vue
💁♂️ If no "layout" property, "default" is assumed
// nuxt.config.js
export default {
loading: '~/components/loading.vue'
}
// pages/error.vue
<template>
<div class="container">
<h1 v-if="error.statusCode === 404">Page not found</h1>
<h1 v-else>An error occurred</h1>
<nuxt-link to="/">Home page</nuxt-link>
</div>
</template>
<script>
export default {
props: ['error'],
}
</script>
Get in touch! david.guijarro@share-now.com twitter.com/davguij
https://slides.com/davguij/ijs-nuxtjs