Book Image

Vue.js 2 Cookbook

By : Andrea Passaglia
Book Image

Vue.js 2 Cookbook

By: Andrea Passaglia

Overview of this book

Vue.js is an open source JavaScript library for building modern, interactive web applications. With a rapidly growing community and a strong ecosystem, Vue.js makes developing complex single page applications a breeze. Its component-based approach, intuitive API, blazing fast core, and compact size make Vue.js a great solution to craft your next front-end application. From basic to advanced recipes, this book arms you with practical solutions to common tasks when building an application using Vue. We start off by exploring the fundamentals of Vue.js: its reactivity system, data-binding syntax, and component-based architecture through practical examples. After that, we delve into integrating Webpack and Babel to enhance your development workflow using single file components. Finally, we take an in-depth look at Vuex for state management and Vue Router to route in your single page applications, and integrate a variety of technologies ranging from Node.js to Electron, and Socket.io to Firebase and HorizonDB. This book will provide you with the best practices as determined by the Vue.js community.
Table of Contents (19 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Dedication
Preface

Using mixins in your components


In Vue and in JavaScript in general, there is no general way to have inheritance as intended in programming. Vue nonetheless has some means of recycling the same features for more components. In this recipe, you will give superpowers to your components, but you will write the powers only once.

Getting ready

This recipe is fairly advanced; it uses some nasty tricks that are very useful to understand how Vue works and may be helpful as a workaround in some situations. Anyway, it is not recommended if you don't already have some experience with Vue.

How to do it...

First, we will create two regular elements: the first will represent a man--you can use the man emoji:

Vue.component('man', { 
  template: '<p>
man</p>' })

Well, that was simple. Next, we will create a cat component:

Vue.component('cat', { 
  template: '<p>
cat</p>' })

After those, you can instantiate Vue like this:

new Vue({ 
  el: '#app' 
})

In your HTML, you compose all the three with...