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

Creating a functional component


A lighter version of a component is a functional component. The functional component doesn't have instance variables (so no this) and has no state. In this recipe, we will write a simple functional component that takes some instructions via HTML and turns them into a drawing.

Getting ready

Before attempting this recipe, you should at least become familiar with the render function in Vue. You can use the previous recipes to do that.

How to do it...

When you are writing an <svg> element, you usually have to put data in the attributes of elements inside it to actually draw shapes. For example, if you want to draw a triangle, you have to write the following:

<svg>
  <path d="M 100 30 L 200 30 L 150 120 z"/>
</svg>

The text inside the d attribute is a series of instructions that make a virtual cursor move to draw: M moves the cursor to the (100, 30) coordinate inside the <svg>, then L traces a line up until (200, 30) and then again to the...