Nuxt.js - Introduction

45

Transcript of Nuxt.js - Introduction

Page 1: Nuxt.js - Introduction
Page 2: Nuxt.js - Introduction

A minimalist framework for server-rendered Vue.js applications

Inspired by Next.js

Page 3: Nuxt.js - Introduction

Built on top of Vue.js 2

Page 4: Nuxt.js - Introduction

Server-side RenderingWith Node.js

Page 5: Nuxt.js - Introduction

ES6 TranspilationWith Babel

Page 6: Nuxt.js - Introduction

Code SplittingWith Webpack

Page 7: Nuxt.js - Introduction

Focus on writing*.vue files

Page 8: Nuxt.js - Introduction

Example

Page 9: Nuxt.js - Introduction

npm install --save nuxt

Page 10: Nuxt.js - Introduction

package.json{ “dependencies": { “nuxt”: "latest" }, “scripts”: { “dev”: “nuxt" }}

Page 11: Nuxt.js - Introduction

pages/index.vue<template> <h1>Hello {{ name }}</h1></template>

<script>export default { data () { return { name: ‘world’ } }}</script>

Page 12: Nuxt.js - Introduction

npm run dev

Page 13: Nuxt.js - Introduction

http://localhost:3000

Page 14: Nuxt.js - Introduction

<h1>Hello world</h1>

Page 15: Nuxt.js - Introduction

pages/ is the main API

Page 16: Nuxt.js - Introduction

pages/index.vue —> / pages/about.vue —> /about

Page 17: Nuxt.js - Introduction

Async Data

Page 18: Nuxt.js - Introduction

pages/index.vue<template> <ul> <li v-for=“post in posts”> {{ post.title }} </li> </ul></template>

<script>import axios from 'axios'

export default { data () { return axios.get(‘https://my-api') .then((res) => { return { posts: res.data } }) }}</script>

Page 19: Nuxt.js - Introduction

Server Data

Page 20: Nuxt.js - Introduction

pages/index.vue<template> <h1>Hi from {{ name }}</h1></template>

<script>export default { data ({ req }) { return { name: (req ? ‘server' : ‘client' } } }}</script>

Page 21: Nuxt.js - Introduction

data(context)

- isServer - isClient - isDev - req - res - params - query

- env - route - base - store - error({ statusCode, message }) - redirect([status], path, [query])

Page 22: Nuxt.js - Introduction

pages/auth.vue<template> <h1>Authenticated</h1></template>

<script>export default { data ({ store, redirect }) { if (store.user.authUser) return redirect(‘/login’) return {} }}</script>

Page 23: Nuxt.js - Introduction

Customisablenuxt.config.js

Page 24: Nuxt.js - Introduction

Custom RoutesEx: pages/users/_id.vue -> /users/:id

Page 25: Nuxt.js - Introduction

Vue pluginsEx: vuelidate, vue-i18n, etc.

Page 26: Nuxt.js - Introduction

nuxt.config.jsmodule.exports = { plugins: [ ‘plugins/vuelidate.js’ ], build: { vendor: [‘vuelidate’] }}

Page 27: Nuxt.js - Introduction

plugins/vuelidate.js

import Vue from 'vue'import Validation from 'vuelidate'

Vue.use(Validation)

Page 28: Nuxt.js - Introduction

Head elementstitle, meta tags, etc.

Page 29: Nuxt.js - Introduction

nuxt.config.jsmodule.exports = { head: { title: ‘Default title’, meta: [ { charset: ‘utf-8’ } ] }}

Page 30: Nuxt.js - Introduction

pages/index.vue<template> <h1>Hello world</h1></template>

<script>export default { head: { title: ‘Index page' }}</script>

Page 31: Nuxt.js - Introduction

Vuex StoreWith store/index.js

Page 32: Nuxt.js - Introduction

router/index.jsimport Vuex from 'vuex'

const store = new Vuex.Store({ state: { counter: 0 }, mutations: { increment (state) { state.counter++ } }})

export default store

Page 33: Nuxt.js - Introduction

pages/index.vue

<template> <button @click=“$store.commit(‘increment’)"> {{ $store.state.counter }} </button></template>

Page 34: Nuxt.js - Introduction

Layoutslayouts/app.vue

Page 35: Nuxt.js - Introduction

layouts/app.vue<template> <nuxt-container> <p>My nav bar here!</p> <nuxt></nuxt> </nuxt-container></template>

<style src=“~assets/main.css”></style>

Page 36: Nuxt.js - Introduction

Etc.- Routes Transitions - Static files - Error page - Testing - Environment variables - postcss plugins - babel configuration - webpack config

Page 37: Nuxt.js - Introduction

Use it Programmatically

Page 38: Nuxt.js - Introduction

server.js

const Nuxt = require(‘nuxt')const nuxt = new Nuxt(options)

nuxt.build().then(() => { // nuxt.renderRoute(‘/‘)})

Page 39: Nuxt.js - Introduction

Express Middlewareapp.use(nuxt.render)

Page 40: Nuxt.js - Introduction

server.jsconst Nuxt = require(‘nuxt’)const app = require(‘express’)()

new Nuxt(options).then((nuxt) => { app.use(nuxt.render) app.listen(3000)})

Page 41: Nuxt.js - Introduction

Server Deployment

Page 42: Nuxt.js - Introduction

package.json{ “dependencies": { “nuxt”: "latest" }, “scripts”: { “dev”: “nuxt”, “build”: “nuxt build”, “start”: “nuxt start" }}

Page 43: Nuxt.js - Introduction

Serverless Deploymentnuxt generate

Page 44: Nuxt.js - Introduction

pages/index.vue —> /index.html

pages/about.vue —> /about/index.html

Page 45: Nuxt.js - Introduction

nuxtjs.org

Chopin brothers

Made with nuxt.js