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 JSX to render a component


JSX is very popular in the React community. In Vue, you don't have to use JSX to build templates for your components; you can use the much more familiar HTML. JSX, however, is the next best thing you can do if you are forced to write a lot of render functions.

Getting ready

Before venturing into this recipe, you better play a little with the render function. The previous recipes provide some exercises.

How to do it...

JSX needs a Babel plugin to work. For this recipe, I will assume that you are working within the webpack template.

To install the babel plugin, you can run the following command:

npm install
  babel-plugin-syntax-jsx
  babel-plugin-transform-vue-jsx
  babel-helper-vue-jsx-merge-props
  --save-dev

Inside the .babelrc file, add the following in the plugins array:

"plugins": [
  ...
  "transform-vue-jsx"
]

Run npm install as usual to actually install all the dependencies.

Now, open the main.js and delete everything inside. Replace it with the following code...