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

Formatting currencies with filters


Formatting currencies in Vue 1 was somewhat limited; we will be using the excellent accounting.js library to build a much more powerful filter.

Getting ready

The basics of filtering are explored in the Formatting your text with filters recipe; where you build a basic filter ensure that you complete that, then come back here.

How to do it...

Add accounting.js to your page. Refer to http://openexchangerates.github.io/accounting.js/ for more details on how to do it. If you are using JSFiddle though, you can just add it as an external resource to the left menu. You can add a link to CDN, which is serving it, for example, https://cdn.jsdelivr.net/accounting.js/0.3.2/accounting.js.

This filter will be extremely simple:

Vue.filter('currency', function (money) { 
  return accounting.formatMoney(money) 
})

You can try it out with a one-liner in HTML:

I have {{5 | currency}} in my pocket

It will default to dollars, and it will print I have $5.00 in my pocket.

How it works...