Skip to content

Commit 4fc5077

Browse files
committed
2 parents fda7609 + f51855c commit 4fc5077

File tree

1 file changed

+53
-7
lines changed

1 file changed

+53
-7
lines changed

README.md

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ List of 300 VueJS Interview Questions
185185
|176| [What is the default namespace behavior in vuex?](#what-is-the-default-namespace-behavior-in-vuex)|
186186
|177| [When do you reuse modules?](#when-do-you-reuse-modules)|
187187
|178| [What are the principles enforced by vuex?](#what-are-the-principles-enforced-by-vuex)|
188+
|179| [Can I perform mutations directly in strict mode?](#can-i-perform-mutations-directly-in-strict-mode)|
189+
|180| [How to use model directive with two way computed property?](#how-to-use-model-directive-with-two-way-computed-property)|
188190

189191
1. ### What is VueJS?
190192
**Vue.js** is an open-source, progressive Javascript framework for building user interfaces that aim to be incrementally adoptable. The core library of VueJS is focused on the `view layer` only, and is easy to pick up and integrate with other libraries or existing projects.
@@ -3107,11 +3109,11 @@ List of 300 VueJS Interview Questions
31073109
}, 1000)
31083110
})
31093111
},
3110-
actionTwo ({ dispatch, commit }) {
3111-
return dispatch('actionA').then(() => {
3112-
commit('second mutation')
3113-
})
3114-
}
3112+
actionTwo ({ dispatch, commit }) {
3113+
return dispatch('actionA').then(() => {
3114+
commit('second mutation')
3115+
})
3116+
}
31153117
}
31163118
```
31173119
As per the above example, When you try to dispatch actionTwo it dispatchs actionOne first and then commits respective mutation. You can still simplify with async/await as below,
@@ -3204,6 +3206,50 @@ List of 300 VueJS Interview Questions
32043206
1. The Application-level state need to be centralized in the store
32053207
2. The state should be mutated by committing mutations only(i.e, for synchronous transactions)
32063208
3. The actions should be used for asynchronous transactions
3207-
179. ### ?
3208-
180. ### ?
3209+
179. ### Can I perform mutations directly in strict mode?
3210+
In strict mode, you can't mutate state directly using `v-model` attribute. If you use v-model it throws an error because mutation is not performed inside an explicit Vuex mutation handler.
3211+
For example, the below input throws an error due to v-model usage
3212+
```javascript
3213+
<input v-model="stateObject.message">
3214+
```
3215+
In this case, you need to bind the <input>'s value. It can be resolved using value attribute as below,
3216+
```javascript
3217+
<input :value="username" @input="updateProfile">
3218+
3219+
computed: {
3220+
...mapState({
3221+
message: state => state.user.username
3222+
})
3223+
},
3224+
methods: {
3225+
updateProfile (e) {
3226+
this.$store.commit('updateProfile', e.target.value)
3227+
}
3228+
},
3229+
mutations: {
3230+
updateProfile (state, username) {
3231+
state.user.username = username
3232+
}
3233+
}
3234+
```
3235+
180. ### How to use model directive with two way computed property?
3236+
You can still use model directive using two-way computed property with a setter.
3237+
```javascript
3238+
<input v-model="username">
3239+
computed: {
3240+
message: {
3241+
get () {
3242+
return this.$store.state.user.username
3243+
},
3244+
set (value) {
3245+
this.$store.commit('updateProfile', value)
3246+
}
3247+
}
3248+
}
3249+
mutations: {
3250+
updateProfile (state, username) {
3251+
state.user.username = username
3252+
}
3253+
}
3254+
```
32093255

0 commit comments

Comments
 (0)