⚠️ This lesson is retired and might contain outdated information.

Handle Forms Connected to a Vuex Store using TypeScript

Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 7 months ago

v-model uses 2 way binding to make local state easy to update and connect variables to an input or component, in an implicit way. At the same time, that makes it difficult to combine it with Vuex, since getters and mutations/actions must be called explicitly.

This lesson shows you how you can apply the v-model functionality to a Vuex store by doing it in explicitly in 2 steps on a class-based component using TypeScript.

We're going to implement the check feature for the to-dos. For that, let's try view model. First go here and write an input checkbox. Same view model equal to-do checked. Fix the indentation. Let's copy this input back, and paste it further down this list. Then change the view model to done.

Let's try this. Click on the checkboxes, and you see they are changing. We may think this is working correctly, but if we check the state, let's check for example the getters. The state doesn't change at all. This is because view model is changing the local state of that variable. What we want to do is to trigger a motation.

Go to the to-dos file, where we have the motations, and add toggle to-do. Then the only thing we have to do is to invert the checked property. Now, we need to go back to the app view component, and declare the toggle to-do motation.

What we have to do is, instead of using view model, do it in two steps. First, let's use the checked property to bind that value. Then use the change event to call the motation. In this way, we are replicating the view model functionality, but by controlling ourselves how to get the data from, and what to do to change it.

Don't forget to update the DOMs list. Let's try this again. Scroll down to the getters, and then click the checkboxes, and we'll see the state is changing. This is working as we expect.

egghead
egghead
~ 8 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today