Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Front-End Development Projects with Vue.js
  • Table Of Contents Toc
Front-End Development Projects with Vue.js

Front-End Development Projects with Vue.js

By : Raymond Camden, Hugo Di Francesco, Clifford Gurney , Philip Kirkbride , Maya Shavin , Dániel Szabó
4.8 (10)
close
close
Front-End Development Projects with Vue.js

Front-End Development Projects with Vue.js

4.8 (10)
By: Raymond Camden, Hugo Di Francesco, Clifford Gurney , Philip Kirkbride , Maya Shavin , Dániel Szabó

Overview of this book

Are you looking to use Vue 2 for web applications, but don't know where to begin? Front-End Development Projects with Vue.js will help build your development toolkit and get ready to tackle real-world web projects. You'll get to grips with the core concepts of this JavaScript framework with practical examples and activities. Through the use-cases in this book, you'll discover how to handle data in Vue components, define communication interfaces between components, and handle static and dynamic routing to control application flow. You'll get to grips with Vue CLI and Vue DevTools, and learn how to handle transition and animation effects to create an engaging user experience. In chapters on testing and deploying to the web, you'll gain the skills to start working like an experienced Vue developer and build professional apps that can be used by other people. You'll work on realistic projects that are presented as bitesize exercises and activities, allowing you to challenge yourself in an enjoyable and attainable way. These mini projects include a chat interface, a shopping cart and price calculator, a to-do app, and a profile card generator for storing contact details. By the end of this book, you'll have the confidence to handle any web development project and tackle real-world front-end development problems.
Table of Contents (16 chapters)
close
close
Preface

Computed Properties

Computed properties are a unique data type that will reactively update when source data used within the property is updated. They may look like a Vue method, but they are not. In Vue, we can track changes to a data property by defining them as a computed property, add custom logic within this property, and use it anywhere within the component to return a value. Computed properties are cached by Vue, making them more performant for returning data than a data prop or using a Vue method.

Instances where you may use a computed property include but are not limited to:

  • Form validation:

    In this example, an error message will appear when the total data property is less than 1. The computed property for total will update every time a new piece of data is added to the items array:

    <template>
        <div>{{errorMessage}}</div>
    </template>
    <script>
        export default {
            data() {
                return {
                    items: []
                }
            },
            computed: {
                total() {
                    return this.items.length
                },
                errorMessage() {
                    if (this.total < 1) {
                        return 'The total must be more than zero'
                    } else {
                        return ''
                    }
                }
            }
        }
    </script>

    This will generate the following output:

    The total must be more than zero
  • Combining data props:

    In the following example, you can use computed props to combine two pieces of data into one returnable string, formalName, that can be used within your component:

    <template>
        <div>{{ formalName }}</div>
    </template>
    <script>
        export default {
            data() {
                return {
                    title: 'Mr.',
                    surname: 'Smith'
                }
            },
            computed: {
                formalName() {
                    return this.title + ' ' + this.surname
                }
            }
        }
    </script>

    This will generate the following output:

    Mr. Smith
  • Outputting complex information into the Vue template:

    In this more complicated example, we use computed properties to break down the large data object called post. You will use the simplified and semantic computed properties to output the information into your components template. The computed properties in this example make it easier to identify and use the author's full name, see how many posts they have produced, and have the data to display their featured post:

    <template>
        <div>
            <p>{{ fullName }}</p>
            <p>{{ totalPosts }}</p>
            <p>{{ featuredPosts }}</p>
        </div>
    </template>
    <script>
        export default {
            data() {
                return {
                    post: {
                        fields: {
                            author: {
                                firstName: 'John',
                                lastName: 'Doe'
                            },
                            entries: [{
                                    title: "Entry 1",
                                    content: "Entry 1's content",
                                    featured: true
                                },
                                {
                                    title: "Entry 2",
                                    content: "Entry 2's content",
                                    featured: false
                                }
                           ]
                        }
                    }
                }
            },
            computed: {
                fullName() {
                    // Return string
                    return this.post.fields.author.firstName + ' ' +                  this.post.fields.author.lastName
                },
                totalPosts() {
                    // Return number
                    return this.post.fields.entries.length
                },
                featuredPosts() {
                    // Return string
                    return this.post.fields.entries.filter(entry => {
                        // If featured is true, return the entry title
                        if (entry.featured) {
                            return entry
                        }
                    })
                }
            }
        }
    </script>

    This will generate the following output:

    Figure 2.1: The computed name output

Figure 2.1: The computed name output

Computed properties are very valuable to a Vue developer when creating performant components. In the next exercise, we will explore how to use this inside of a Vue component.

Exercise 2.01: Implementing Computed Data into a Vue Component

In this exercise, you will use a computed property to help cut down the amount of code you need to write inside your Vue templates by concisely outputting basic data. To access the code files for this exercise, refer to https://packt.live/3n1fQZY.

  1. Open a command-line terminal, navigate into the Exercise 2.01 folder, and run the following commands in order:
    > cd Exercise2.01/
    > code .
    > yarn
    > yarn serve

    Go to https://localhost:8080.

  2. Create an input field for the first name, use v-model to bind the data prop firstName to this field:
    <input v-model="firstName" placeholder="First name" />
  3. Create a second input field for the last name, and use v-model to bind the data prop lastName to this field:
    <input v-model="lastName" placeholder="Last name" />
  4. Include these new v-model data props in the Vue instance by returning them in the data() function:
    data() {
        return {
          firstName: '',
          lastName: '',
        }
      },
  5. Create a computed data variable called fullName:
    computed: {
        fullName() {
          return `${this.firstName} ${this.lastName}`
        },
      },
  6. Underneath your input fields, using heading tags, output the computed data:
    <h3 class="output">{{ fullName }}</h3>

    This will generate the following output:

    Figure 2.2: Output of the computed data will show the first and last name

Figure 2.2: Output of the computed data will show the first and last name

In this exercise, we saw how we can write an expression inside a computed data property using data from the v-model and combine the first name and last name into a single output variable that can be reused.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Front-End Development Projects with Vue.js
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon