Thursday, March 21, 2019

The Difference Between Computed Properties, Methods and Watchers in Vue

The Difference Between Computed Properties, Methods and Watchers in Vue

For those starting out learning Vue, there’s a bit of confusion over the difference between methods, computed properties and watchers.

Even though it’s often possible to use each of them to accomplish more or less the same thing, it’s important to know where each outshines the others.

In this quick tip, we’ll look at these three important aspects of a Vue application and their use cases. We’ll do this by building the same search component using each of these three approaches.

Methods

A method is more or less what you’d expect — a function that’s a property of an object. You use methods to react to events which happen in the DOM, or you can call them from elsewhere within your component — for example, from within a computed property or watcher. Methods are used to group common functionality — for example, to handle a form submission, or to build a reusable feature such as making an Ajax request.

You create a method in a Vue instance, inside the methods object:

new Vue({
  el: "#app",
  methods: {
    handleSubmit() {}
  }
})

And when you want to make use of it in your template, you do something like this:

<div id="app">
  <button @click="handleSubmit">
    Submit
  </button>
</div>

We use the v-on directive to attach the event handler to our DOM element, which can also be abbreviated to an @ sign.

The handleSubmit method will now get called each time the button is clicked. For instances when you want to pass an argument that will be needed in the body of the method, you can do this:

<div id="app">
  <button @click="handleSubmit(event)">
    Submit
  </button>
</div>

Here we’re passing an event object which, for example, would allow us to prevent the browser’s default action in the case of a form submission.

However, as we’re using a directive to attach the event, we can make use of a modifier to achieve the same thing more elegantly: @click.stop="handleSubmit".

Now let’s see an example of using a method to filter a list of data in an array.

In the demo, we want to render a list of data and a search box. The data rendered changes whenever a user enters a value in the search box. The template will look like this:

<div id="app">
  <h2>Language Search</h2>

  <div class="form-group">
    <input
      type="text"
      v-model="input"
      @keyup="handleSearch"
      placeholder="Enter language"
      class="form-control"
    />
  </div>

  <ul v-for="(item, index) in languages" class="list-group">
    <li class="list-group-item" :key="item"></li>
  </ul>
</div>

As you can see, we’re referencing a handleSearch method, which is called every time the user types something into our search field. We need to create the method and data:

new Vue({
  el: '#app',
  data() {
    return {
      input: '',
      languages: []
    }
  },
  methods: {
    handleSearch() {
      this.languages = [
        'JavaScript',
        'Ruby',
        'Scala',
        'Python',
        'Java',
        'Kotlin',
        'Elixir'
      ].filter(item => item.toLowerCase().includes(this.input.toLowerCase()))
    }
  },
  created() { this.handleSearch() }
})

The handleSearch method uses the value of the input field to update the items that are listed. One thing to note is that within the methods object, there’s no need to reference the method with this.handleSearch (as you’d have to do in React).

See the Pen Vue Methods by SitePoint (@SitePoint) on CodePen.

The post The Difference Between Computed Properties, Methods and Watchers in Vue appeared first on SitePoint.


by Kingsley Silas via SitePoint

Instagram will soon Allow you to Change your Username while Keeping the old one Saved...but for how long?

Has it ever happened that you thought about changing your Instagram username and try out something new but the fear of losing your old username to someone else for good stopped you? Fear no more, because Instagram is all set to help you out with this. Jane Manchun Wong, the famous code hacker...

[ This is a content summary only. Visit our website https://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Ali Siddiqui via Digital Information World

Hoaxes On Facebook Are Creating Confusion Among Users

Facebook is one of the most popular social media sites and often misused by scammers to attack maximum users. Hoaxes are a common practice on Facebook, just like the recent one, in which apparently Mark Zuckerberg is sending private messages to users. As noted by Matt Navarra, a message has been...

[ This is a content summary only. Visit our website https://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Aqsa Rasool via Digital Information World

Google Unwittingly Runs Fake Ad, Causes Tech-Support Scam

Google is known for being a hub of advertising. After all, it is the owner of the single most visited website in the world, so pretty much every brand out there would be eager to get their wares displayed prominently on the platform thus allowing them to gain access to a much wider variety of users...

[ This is a content summary only. Visit our website https://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Zia Zaidi via Digital Information World

5 P’s of Social Media Marketing That Your Business Should Focus On (infographic)

Do you ever wonder why some brands outperform others on social media? Let’s take a simple example of the famous furniture brand IKEA. When it comes to furniture, we wonder why IKEA is one of the most reliable furniture brand compared to other similar famous brands in the market? Now, as an...

[ This is a content summary only. Visit our website https://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Web Desk via Digital Information World

Netflix Interactive TV Data is Being Used to Know About Users, even More, Study Revealed

Netflix introduced interactive TV to give users a whole new experience, but users seem more furious about the chances of a leak of information. Research from Indian Institute of Technology, Madras (IITM) has claimed that interactive TV allows deducing the choices of viewers. Netflix has been...

[ This is a content summary only. Visit our website https://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Aqsa Rasool via Digital Information World

Samsung - Do what you can’t

Samsung - Do what you can’t
An immersive interactive film to showcase Samsung’s new product line.
by via Awwwards - Sites of the day