David-Licen Nuxt
David-Licen Nuxt
v1.0.0-alpha1
Highlights
• Vue-Router
• Vuex (included only when A total of only 28kb
using the store option) min+gzip (31kb with Vuex)
• Vue-Meta
Features
• Bundling and minifying of
• Automatic Code Splitting
your JS & CSS
• Server-Side Rendering • Managing Head Elements
• Powerful Routing System
• Hot reloading in
with Asynchronous Data
Development
• Static File Serving • Error handling (404 pages)
We’re gonna cover:
• Set-up • Deployment
• Routing • Assets
• Views • Plugins
• Async Data • etc
Let’s roll!
Using a starter template
<script>
export default {
data () {
return {
name: 'Vuers'
}
}
}
</script>
npm run dev
http://localhost:3000
Result:
<h1>Hello Vuers!</h1>
Routing
pages/ is the main API
Nuxt.js automatically generates the
vue-router configuration according
to your file tree of *.vue files inside
the /pages directory
pages/
--| index.vue ⟶ /
--| about.vue ⟶ /about
--| contact.vue ⟶ /contact
{
"routes": [{
"name": "index",
"path": "/",
"component": "pages/index.vue"
},
{
"name": "about",
"path": "/",
"component": "pages/about.vue"
},
{
"name": "contact",
"path": "/",
"component": "pages/contact.vue"
}]
}
Links
<nuxt-link to="/about">About</nuxt-link>
<nuxt-link to="/contact">Contact</nuxt-link>
Dynamic Routes
pages/
--| users/
-----| _id.vue
{
"routes": [{
"name": "users-id",
"path": "/users/:id?",
"component": "pages/users/_id.vue"
}]
}
Many more about routing
(& page transitions):
https://nuxtjs.org/guide/routing
Views
The default template (app.html)
<!DOCTYPE html>
<html {{ HTML_ATTRS }}>
<head>
{{ HEAD }}
</head>
<body {{ BODY_ATTRS }}>
{{ APP }}
</body>
</html>
Customize Conditional classes for IE
<!DOCTYPE html>
<!--[if IE 9]><html lang="en-US" class=“lt-ie9 ie9”
{{ HTML_ATTRS }}><![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--><html {{ HTML_ATTRS }}>
<!--<![endif]-->
<head>
{{ HEAD }}
</head>
<body {{ BODY_ATTRS }}>
{{ APP }}
</body>
</html>
layouts/default.vue
<template>
<nuxt/>
</template>
layouts/default.vue
<template>
<div>
<div id="skip">
<a href="#main-menu" class=“visually-hidden skip-link">
Skip to main navigation
</a>
</div>
<nuxt/>
</div>
</template>
pages/index.vue
<template></template>
<script>
export default {
data () {
return { title: 'Hello World!' }
},
head () {
return {
title: this.title,
meta: [{ name: 'description', content: 'My description' }]
}
},
fetch () { ... },
}
</script>
Other attributes
• asyncData • transition
• fetch • scrollToTop
• head • validate
• layout • middleware
Async Data
Async Data
• Nuxt.js adds an asyncData() method to let
you handle async operations before setting
the component data.
• The result from asyncData will be merged
with data.
Different ways to use asyncData
• Returning a Promise.
• Using the async/await proposal
• Define a callback as second argument
Returning a Promise
export default {
asyncData ({ params }) {
return axios.get(`https://my-api/posts/${params.id}`)
.then((res) => {
return { title: res.data.title }
})
}
}
Using async/await
export default {
async asyncData ({ params }) {
let { data } = await
axios.get(`https://my-api/posts/${params.id}`)
return { title: data.title }
}
}
Using a callback
export default {
asyncData ({ params }, callback) {
axios.get(`https://my-api/posts/${params.id}`)
.then((res) => {
callback(null, { title: res.data.title })
})
}
}
Deployment
Server Deployment
becomes:
require('~assets/image.png')
Served Assets
<template>
<img src="~assets/image.png">
</template>
is complied to:
createElement('img', { attrs: { src:
require('~assets/image.png') }})
Static Assets
<img src="/my-image.png"/>
Plugins
External packages
Install via npm:
npm install --save axios
nuxt.config.js:
module.exports = {
plugins: ['~plugins/vue-notifications']
}
nuxtjs.org
Thank you!
Any ?