Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Vuex Quick Start Guide

You're reading from   Vuex Quick Start Guide Centralized State Management for your Vue.js applications

Arrow left icon
Product type Paperback
Published in Apr 2018
Publisher Packt
ISBN-13 9781788999939
Length 152 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Andrea Koutifaris Andrea Koutifaris
Author Profile Icon Andrea Koutifaris
Andrea Koutifaris
Arrow right icon
View More author details
Toc

Form handling restrictions when using Vuex

Using Vue's v-model feature with the Vuex state results in a direct modification of the state, which is forbidden.

Take a look at the following example:

<input v-model="$store.state.message">

In this example, $store.state is mutated directly by v-model, and if strict mode is enabled, it will result in an error being thrown.

There is more than one way to solve this problem, and I will show you the one that, in my opinion, is better: You can use a mutable computed property that accesses the state property when read and commits a mutation when set:

<input v-model="message">
// ...
computed: {
message: {
get () {
return this.$store.state.obj.message;
},
set (value) {
this.$store.commit('updateMessage', value);
}
}
}

Using a mutable computed property also allows you to add...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime