Vue.js 2 Design Patterns and Best Practices

Book description

Become an expert Vue developer by understanding the design patterns and component architecture of Vue.js to write clean and maintainable code.

About This Book

  • Craft highly modular applications by exploring the design patterns and component architecture of Vue.js
  • Enforce a Flux-like application architecture in your Vue.js applications with Vuex
  • Easy-to-follow examples that can be used to create reusable code and extensible designs

Who This Book Is For

This book targets Vue Developers who care about framework design principles and utilize commonly found design patterns in developing web applications.

What You Will Learn

  • Understand the theory and patterns of Vue.js
  • Build scalable and modular Vue.js applications
  • Take advantage of Vuex for reactive state management.
  • Create Single Page Applications with vue-router.
  • Use Nuxt for FAST server side rendered Vue applications.
  • Convert your application to a Progressive Web App (PWA) and add ServiceWorkers, offline support, and more
  • Build your app with Vue.js by following up with best practices and explore the common anti-patterns to avoid

In Detail

The book starts by comparing Vue.js with other frameworks and setting up the development environment for your application, and gradually move on to writing and styling clean, maintainable, and reusable components that can be used across your application.

Further on, you'll look at common UI patterns, Vue form submission, and various modifiers such as lazy binding, number typecasting, and string trimming to create better UIs. You will also explore best practices for integrating HTTP into Vue.js applications to create an application with dynamic data.

Routing is a vitally important part of any SPA, so you will focus on the Vue router and explore routing a user between multiple pages. Next, you'll also explore state management with Vuex, write testable code for your application, and create performant, server-side rendered applications with Nuxt.

Towards the end, we'll look at common antipatterns to avoid, to save you from a lot of trial and error and development headaches.

By the end of this book, you'll be well on your way to becoming an expert Vue developer who can leverage design patterns to efficiently architect the design of your application and write clean and maintainable code.

Style and approach

This easy-to-follow practical guide will help you develop efficient Vue.js apps by following best practices and using common design patterns.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Publisher resources

Download Example Code

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Vue.js 2 Design Patterns and Best Practices
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Vue.js Principles and Comparisons
    1. Prerequisites
      1. Windows
      2. Mac
        1. Installing Node via Homebrew
      3. Editor
      4. Browser
        1. Installing the Vue devtools
      5. Vue CLI
    2. How Vue.js compares
      1. React
      2. Angular
      3. Mobile development
      4. Server-Side Rendering (SSR)
      5. Conclusion
    3. Summary
  7. Proper Creation of Vue Projects
    1. Visual Studio Code extensions
      1. Vetur
      2. Vue 2 Snippets
    2. Vue CLI
      1. JavaScript modules
        1. Vue-loader
      2. Loading modules without Webpack
    3. VueJS devtools
    4. TypeScript and Vue
      1. Lifecycle hooks
      2. Properties
      3. Computed
    5. RxJS and Vue
      1. What is RxJS?
        1. Integrating with Vue
    6. Summary
    7. Proxying
      1. How 'this' works within JavaScript
      2. How Vue handles 'this'
    8. Data properties
    9. Computed properties
    10. Watched properties
    11. Lifecycle hooks
    12. Vue.js and the Virtual DOM
      1. DOM
      2. Virtual DOM
    13. Summary
  8. Writing Clean and Lean Code with Vue
    1. Proxying
      1. How 'this' works within JavaScript
      2. How Vue handles 'this'
    2. Data properties
    3. Computed properties
    4. Watched properties
    5. Lifecycle hooks
    6. Vue.js and the Virtual DOM
      1. DOM
      2. Virtual DOM
    7. Summary
  9. Vue.js Directives
    1. Model
    2. Iteration with v-for
    3. Bindings
      1. Adding secondary properties
      2. Style bindings
    4. DOM events and v-on
      1. Key modifiers
      2. Event modifiers
    5. Conditionally showing DOM elements
      1. v-show
      2. v-if
      3. v-else
      4. v-else-if
    6. Filters
      1. Locally registered filters
      2. Globally registered filters
    7. Summary
  10. Secured Communication with Vue.js Components
    1. Your first Vue component
      1. Registering components globally
      2. Scoped styles
      3. Registering a component locally
    2. Component communication
      1. Configuring property values
      2. Custom events
      3. Sending event values
    3. Event Bus
    4. Slots
      1. Defaults
      2. Named slots
    5. Summary
  11. Creating Better UI
    1. Animations
    2. CSS animations
      1. Animate.css
        1. Using Animate.css
    3. Transitions
      1. Transition states
    4. Form validation
      1. What is Vuelidate?
      2. Using Vuelidate
        1. Displaying form errors
        2. Password validation
        3. Form submission
    5. Render/functional components
      1. Rendering elements
        1. Attributes
        2. Components and props
        3. JSX
    6. Summary
  12. HTTP and WebSocket Communication
    1. HTTP
      1. Installing JSON server
      2. HTTP GET
      3. HTTP POST
      4. HTTP PUT
      5. HTTP DELETE
    2. Real-time chat application with Node and Socket.io
      1. What is Socket.io?
      2. Server setup
      3. Client connections
        1. Setting up Vue and Socket.io
      4. Determining connection status
        1. Creating a connection status bar
      5. Navigation bar
      6. Message list
      7. Adding messages to the list
        1. Server-side events with Socket.io
          1. Nodemon
    3. Summary
  13. Vue Router Patterns
    1. Single Page Applications
    2. Using the router
      1. Creating routes
      2. Dynamic routes
        1. Route props
      3. Component Navigation Guards
        1. beforeRouteUpdate
        2. beforeRouteEnter
        3. beforeRouteLeave
      4. Global router hooks
        1. beforeEach
        2. beforeResolve
        3. afterEach
        4. Resolution stack
      5. Programmatic navigation
        1. router.replace
        2. router.go
      6. Lazy loading routes
    3. An SPA project
      1. Enabling the router
      2. Defining routes
      3. Creating the UserList route
      4. Getting data from an API
      5. Creating a detail page
      6. Child routes
    4. Summary
  14. State Management with Vuex
    1. What is Vuex?
      1. State Management Pattern (SMP)
      2. Thinking about state
    2. Using Vuex
      1. Creating a new store
      2. Defining action types
      3. Actions
      4. Mutations
      5. Getters
      6. Combining elements
      7. Payloads
    3. Vuex and Vue devtools
    4. Modules and scalability
    5. Summary
  15. Testing Vue.js Applications
    1. Why testing?
    2. Unit testing
      1. Setting up vue-test-utils
      2. Creating a TodoList
      3. Writing tests
      4. Vue options
      5. Adding new features
      6. Click events
      7. Testing events
    3. Using Wallaby.js for a better testing experience
    4. Summary
  16. Optimization
    1. Progressive Web Applications (PWAs)
      1. Web application manifest
      2. Testing on a device
    2. Firebase deployment
    3. Continuous Integration (CI)
      1. Unit tests
      2. Creating a Git repository
      3. Connecting to Travis CI
        1. Configuring Travis
      4. Automatic deployment to Firebase
    4. Service worker
    5. Summary
  17. Server-Side Rendering with Nuxt
    1. Nuxt
      1. Creating a Nuxt project
      2. Directory structure
      3. Nuxt configuration
      4. Navigation
        1. Navigating between routes
      5. Layouts
      6. The Mock REST API
      7. asyncData
      8. Categories
      9. Category detail
      10. Error page
      11. Plugins
      12. Adding recipes
      13. Transitions
      14. Building for production
        1. Static
        2. SPA mode
    2. Summary
  18. Patterns
    1. Components
      1. Communication – Anti-pattern
        1. What can we change about this to make it better?
      2. Children mutating props – Anti-pattern
        1. What should we do instead?
        2. Mutating property arrays
      3. Using data as an object - Anti-Pattern
      4. Naming components – Anti-pattern
      5. Template expressions
      6. Pattern – Container/Presentational components
      7. Prop validation
      8. Understanding reactivity
    2. Summary

Product information

  • Title: Vue.js 2 Design Patterns and Best Practices
  • Author(s): Paul Halliday
  • Release date: March 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788839792