Book Image

Vue.js 3 Cookbook

By : Heitor Ramon Ribeiro
Book Image

Vue.js 3 Cookbook

By: Heitor Ramon Ribeiro

Overview of this book

Vue.js is a progressive web framework for building professional user interfaces for your web applications. With Vue.js 3, the frontend framework is reinforced with architectural enhancements, new base languages, new render processes, and separated core components. The book starts with recipes for implementing Vue.js 3’s new features in your web development projects and migrating your existing Vue.js apps to the latest version. You will get up and running with TypeScript with Vue.js and find succinct solutions to common challenges and pitfalls faced in implementing components, derivatives, and animation, through to building plugins, adding state management, routing, and developing complete single-page applications (SPAs). As you advance, you'll discover recipes to help you integrate Vue.js apps with Nuxt.js in order to add server-side rendering capabilities to your SPAs. You'll then learn about the Vue.js ecosystem by exploring modern frameworks such as Quasar, Nuxt.js, Vuex, and Vuetify in your web projects. Finally, the book provides you with solutions for packaging and deploying your Vue.js apps. By the end of this Vue.js book, you'll be able to identify and solve challenges faced in building Vue.js applications and be able to adopt the Vue.js framework for frontend web projects of any scale.
Table of Contents (13 chapters)
Fetching Data from the Web via HTTP Requests
Managing Routes with vue-router
Managing the Application State with Vuex
Directives, Plugins, SSR, and More

Creating the Counter component

Now, follow the instructions to create a custom mixin with vue-class-component:

  1. We need to make a new component called CounterByTen.vue in the src/components folder.
  2. Now, let's start making the script part of the Vue component. We will make a class that will have a variable with the type of a number and a default value of 0; two methods, one for increasing by 10 and another for decreasing by 10; and, finally, a computed property to format the final data:
<script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';

export default class CounterByTen extends Vue {
valueNumber: number = 0;

get formattedNumber() {
return `Your total number is: ${this.valueNumber}`;

increase() {
this.valueNumber += 10;

decrease() {
this.valueNumber -= 10;
  1. It's time to create the template and rendering for this component. The process is the same as for a JavaScript Vue file. We will add the buttons for increasing and decreasing the value and for showing the formatted text:
<legend>{{ this.formattedNumber }}</legend>
<button @click="increase">Increase By Ten</button>
<button @click="decrease">Decrease By Ten</button>
  1. In the App.vue file, we need to import the component we just created:
<div id="app">
<Counter />
<hr />
<CounterByTen />

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import Counter from './components/Counter.vue';
import CounterByTen from './components/CounterByTen.vue';

components: {
export default class App extends Vue {

<style lang="stylus">
font-family 'Avenir', Helvetica, Arial, sans-serif
-webkit-font-smoothing antialiased
-moz-osx-font-smoothing grayscale
text-align center
color #2c3e50
margin-top 60px