Más contenido relacionado

Nuxt.JS Introdruction

  1. A minimalist framework for creating universal server side rendered (SSR) applications
  2. David Ličen, 
 Freelance Front-End Developer 
 Twitter: @davision
 Blog: davidlicen.com
  3. v1.0.0-alpha1 On May 21st 2017:
  4. Highlights • Shares an idea from the Next.js • Similar to Ember tries to pack essential set of tools • UI rendering as main scope (abstracting away the client/server distribution)
  5. How it works? Focus on writing *.vue files Code Splitting with Webpack ES6/ES7 transpilation
  6. What’s in the box? • Vue 2 • Vue-Router • Vuex (included only when using the store option) • Vue-Meta 
 A total of only 28kb 
 min+gzip (31kb with Vuex)
  7. Features • Automatic Code Splitting • Server-Side Rendering • Powerful Routing System with Asynchronous Data • Static File Serving • Bundling and minifying of your JS & CSS • Managing Head Elements • Hot reloading in Development • Error handling (404 pages)
  8. We’re gonna cover: • Set-up • Routing • Views • Async Data • Deployment • Assets • Plugins • etc
  9. Let’s roll!
  10. Using a starter template vue init nuxt/starter <project-name>
  11. npm run dev
  12. http://localhost:3000
  13. Barefoot npm install --save nuxt
  14. package.json { "dependencies": { "nuxt": "latest" }, "scripts": { "dev": "nuxt", "build": "nuxt build" "start": "nuxt start" "generate": "nuxt start" } }
  15. pages/index.vue <template> <h1>Hello {{ name }}!</h1> </template> <script> export default { data () { return { name: 'Vuers' } } } </script>
  16. npm run dev
  17. http://localhost:3000
  18. Result: <h1>Hello Vuers!</h1>
  19. Routing
  20. pages/ is the main API
  21. Nuxt.js automatically generates the vue-router configuration according to your file tree of *.vue files inside the /pages directory
  22. pages/
 --| index.vue ⟶ /
 --| about.vue ⟶ /about 
 --| contact.vue ⟶ /contact
  23. { "routes": [{ "name": "index", "path": "/", "component": "pages/index.vue" }, { "name": "about", "path": "/", "component": "pages/about.vue" }, { "name": "contact", "path": "/", "component": "pages/contact.vue" }] }
  24. Links <nuxt-link to="/about">About</nuxt-link> <nuxt-link to="/contact">Contact</nuxt-link>
  25. Dynamic Routes define a .vue file OR a directory prefixed by an _ underscore.
  26. pages/ --| users/ -----| _id.vue Dynamic Routes
  27. { "routes": [{ "name": "users-id", "path": "/users/:id?", "component": "pages/users/_id.vue" }] }
  28. https://nuxtjs.org/guide/routing Many more about routing
 (& page transitions):
  29. Views
  30. The default template (app.html) <!DOCTYPE html> <html {{ HTML_ATTRS }}> <head> {{ HEAD }} </head> <body {{ BODY_ATTRS }}> {{ APP }} </body> </html>
  31. 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>
  32. layouts/default.vue <template> <nuxt/> </template>
  33. 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>
  34. 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>
  35. • asyncData • fetch • head • layout • transition • scrollToTop • validate • middleware Other attributes
  36. Async Data
  37. • 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. Async Data
  38. • Returning a Promise. • Using the async/await proposal • Define a callback as second argument Different ways to use asyncData
  39. Returning a Promise export default { asyncData ({ params }) { return axios.get(`https://my-api/posts/${params.id}`) .then((res) => { return { title: res.data.title } }) } }
  40. Using async/await export default { async asyncData ({ params }) { let { data } = await axios.get(`https://my-api/posts/${params.id}`) return { title: data.title } } }
  41. Using a callback export default { asyncData ({ params }, callback) { axios.get(`https://my-api/posts/${params.id}`) .then((res) => { callback(null, { title: res.data.title }) }) } }
  42. Deployment
  43. npm run build npm run start Server Deployment
  44. npm run generate Serverless Deployment
  45. .nuxt
 pages/
 --| index.vue ⟶ /index.html
 --| about.vue ⟶ /about/index.html 
 --| contact.vue ⟶ /contact/index.html
  46. Assets
  47. /assets folder is automatically processed and resolved as module dependencies. url('~assets/image.png') becomes: require('~assets/image.png') Served Assets
  48. <template>
 <img src="~assets/image.png">
 </template> is complied to: createElement('img', { attrs: { src: require('~assets/image.png') }}) Served Assets
  49. Files from /static is automatically served and accessible in your project root URL
 <img src="/my-image.png"/> Static Assets
  50. Plugins
  51. Install via npm: npm install --save axios use it directly in our pages: import axios from 'axios' use build.vendor key in our nuxt.config.js: build: { vendor: ['axios'] } External packages
  52. Setup the plugin before launching the app. Vue Plugins
  53. plugins/vue-notifications.js import Vue from 'vue' import VueNotifications from 'vue-notifications' nuxt.config.js: module.exports = { plugins: ['~plugins/vue-notifications'] }
  54. nuxtjs.org
  55. Thank you!
  56. Any ?