Computed data not updating in Vue - vue.js

I am using quasar framework and vuex for my app. The parent component is rendering child components with the data from vuex store. The child component is contenteditable and if i press enter key on it, the store is updated. But the computed property in parent component is not updating.
Here is my code:
parent-component.vue
<template>
<div>
<div v-for="(object, objKey) in objects"
:key="objKey">
<new-comp
:is=object.type
:objId="object.id"
></new-comp>
</div>
</div>
</template>
<script>
import ChildComponent from './child-component';
export default {
name: 'ParentComponent',
components: {
ChildComponent
},
computed : {
objects(){
return this.$store.state.objects.objects;
}
},
mounted() {
this.assignEnterKey();
},
methods: {
assignEnterKey() {
window.addEventListener('keydown',function(e) {
if(e.which === 13) {
e.preventDefault();
}
});
}
}
}
child-component.vue
<template>
<div contenteditable="true" #keydown.enter="addChildComp" class="child-container">
Tell your story
</div>
</template>
<script>
export default {
name: 'ChildComponent',
props: ['objId'],
data() {
return {
id: null
}
},
computed : {
serial(){
return this.$store.state.objects.serial;
}
},
methods: {
addChildComp() {
let newId = this.objId + 1;
let newSerial = this.serial + 1;
this.$store.commit('objects/updateObjs', {id: newId, serial: newSerial});
}
}
}
state.js
export default {
serial: 1,
objects: {
1:
{
"id" : 1,
"type" : "ChildComponent",
"content" : ""
}
}
}
mutation.js
export const updateObjs = (state, payload) => {
let id = payload.id;
let serial = payload.serial;
state.objects[serial] = {
"id" : id ,
"type" : "ChildComponent",
"content" : ""
};
}

Vuex mutations follow general Vue.js reactivity rules, this means that Vue.js reactivity traps are applicable to vuex mutations.
In order to maintain reactivity, when adding a property to state.objects you should either:
Use the special Vue.set method:
Vue.set(state.objects, serial, { id, "type" : "ChildComponent", "content" : ""})
Or, recreate state.objects object instead of mutating it:
state.objects = { ...state.objects, [serial]: { id, "type" : "ChildComponent", "content" : ""} }

Related

Get object from Vuex at specific index

I am getting an array from Vuex and I want an object at 2 positions.
HTML
<p class="">
{{ MainImg[2].para}}
</p>
Vue
export default {
name: "App",
components: { },
data() {
return {
imageQuery: this.$route.params.image,
};
},
computed: {
...mapGetters("design", {
MainImg: ["singleDesigns"]
})
},
created() {
this.fetchDesigns();
},
mounted() {
console.log(this.MainImg);
},
methods: {
fetchDesigns() {
this.$store.dispatch("design/getSingleDesign", this.imageQuery);
}
}
};
But it shows an undefined error.
And When I add MainImg array in Vue data like this.
data() {
return {
imageQuery: this.$route.params.image,
MainImg:[{para:"1"},{para:"2"},{para:"3"},{para:"4"}]
};
It Works.
P.S.-
Store Code-
export const state = () => ({
designs: [],
})
export const getters = {
singleDesigns(state) {
return state.designs;
}
}
I am not adding Action and Mutation because it works fine with other code.
It looks like the array is empty at the first rendering, so you should add a condition to render it :
<p class="" v-if="MainImg && MainImg.length >= 2">
{{ MainImg[2].para}}
</p>

Dynamic component by select input /w Vuex store binding

I don't know how to change a dynamic component based on a selectbox option with two-way binding to a Vuex Store.
My single file component (Fieldvalue) houses this dynamic component and also the selectbox.
Its type value comes from a Vuex Store, where it can be found by the fieldId, which is passed through the parent component.
So to a field with an ID like 41, there is a fieldvalue component.
The get() function of fieldtypeComponent has somehow to know the passed through fieldId value from the props, that the parent passes down, to have the ID for searching in the fieldvalues array of the store.
But it does not work how I'm doing it. (commented out lines)
Any ideas?
My Vuex store getter:
export function getFieldvalueTypeByFieldnameId(state){
return (fieldId) => {
const fieldvalue = state.form.fieldvalues.find(e => e.fieldId == fieldId);
return fieldvalue.type;
}
}
Mutation:
export function setFieldvalueType(state, obj){
const { type, fieldId } = obj;
const fieldvalue = state.form.fieldvalues.find( e => e.fieldId === fieldId );
fieldvalue.type = type;
}
My Vuex store state example:
{
"form": {
"name": "",
"fieldvalues": [
{
"formularId": null,
"fieldId": 41,
"value": {},
"type": ""
},
{
"formularId": null,
"fieldId": 44,
"value": {},
"type": ""
}
]
}
}
My single file component:
<template>
<div class="row">
<div class="col-3 self-center">
<q-select
square
filled
v-model="fieldtypeComponent"
:options="fieldtypes"
option-value="component"
map-options
emit-value
label="Type"
/>
</div>
<div class="col">
<template
v-if="fieldtypeComponent === null || fieldtypeComponent == undefined">
<p>no type selected</p>
</template>
<component v-else :is="fieldtypeComponent"></component>
</div>
</div>
</template>
<script>
import IconField from "components/fieldtypes/IconField";
import TextField from "components/fieldtypes/TextField";
export default {
name: "Fieldvalue",
data() {
return {
selected: "",
fieldtypes: [
{ label: "Icon", component: "IconField" },
{ label: "Text", component: "TextField" }
]
};
},
components: {
IconField,
TextField
},
computed: {
fieldtype: function() {
return this.$store.getters["formular/getFieldvalueTypeByFieldnameId"];
},
fieldtypeComponent: {
get() {
//return this.fieldtyp(this.fieldnameId);
//return this.$store.getters["formular/getFieldvalueType"];
return null;
},
set(type) {
const payload = {
type,
fieldId: this.forFieldnameId
};
this.$store.commit("formular/setFieldvalueType", payload);
}
}
},
props: {
forFieldnameId: { type: Number, required: true }
},
methods: {
changeComponent() {
this.fieldtypeComponent = this.selected;
const payload = {
type: this.selected,
fieldId: this.forFieldnameId
};
this.$store.commit("tarife/setFieldvalueTyp", payload);
}
}
};
</script>
Additional info on what I want to do:
I have some fields defined in a formular with a name. To each fieldname from that form I want to save a value later.
But that value (a javascript object/JSON) is different depending on the choosen component from the select input. (TextField or IconField)
I don't know if my way of passing down is right or too complex. Especially regarding to two-way binding or just emitting up to the parent component and let it handle the complete logic.
I need to somehow bind the value from in the chosen component to the Vuex store fieldvalue entry too.
Maybe my approach is wrong and I should do it totally different?

Vue/Vuex dynamic component not changing

I have a vuex store of "nodes". Each one has a type of Accordion or Block.
{
"1":{
"id":1,
"title":"Default title",
"nodes":[],
"type":"Block"
},
"2":{
"id":2,
"title":"Default title",
"nodes":[],
"type":"Accordion"
}
}
When I use the type to create a dynamic component it works great:
<ul>
<li v-for="(node, s) in nodes" :key="parentId + s">
<component :is="node.type" :node="node" :parent-id="parentId"></component>
</li>
</ul>
But when I change it, nothing happens in the view layer:
convert(state, { to, id }) {
state.nodes[id].type = to;
Vue.set(state.nodes[id], "type", to);
},
I even use Vue.set. How can I make this update?
It updates immediately if I then push another node into the array.
CodeSandbox:
https://codesandbox.io/s/romantic-darwin-dodr2?file=/src/App.vue
The thing is that your getter will not work, because it's not pure: Issue. But you can use deep watcher on your state instead:
<template>
<div class="home">
<h1>Home</h1>
<Sections :sections="nodesArr" :parent-id="null"/>
</div>
</template>
<script>
// # is an alias to /src
import Sections from "#/components/Sections.vue";
import { mapState } from "vuex";
export default {
name: "home",
components: {
Sections
},
data: () => {
return {
nodesArr: []
};
},
computed: {
...mapState(["nodes", "root"])
},
watch: {
root: {
handler() {
this.updateArr();
},
deep: true
}
},
mounted() {
this.updateArr();
},
methods: {
updateArr() {
this.nodesArr = this.root.map(ref => this.nodes[ref]);
}
}
};
</script>

Nuxt + Vuex mapGetters value is always undefined

I'm using VueX with Nuxt.JS so let's suppose the following code in the file store/search.js:
export const state = () => ({
results: null
});
export const mutations = {
setResults(state, { results }) {
state.results = results;
}
};
export const actions = {
startSearch({ commit, dispatch }, { type, filters }) {
commit("setResults", { type, filters });
}
};
export const getters = {
results: state => state.results
};
Now in my component results.vue, under the computed property I have something like this:
<template>
<button #click="handleSearch">Search</button>
<div v-if="results && results.length" class="results" >
<div v-for="item in results" :key="item.id">
{{item}}
</div>
</div>
</template>
<script>
import { mapActions, mapGetters } from "vuex";
data() {
return {
selected_type: null,
filters: null
};
},
methods: {
setType(type) {
this.selected_type = type;
this.handleSearch();
},
setFilters(filters) {
this.filters = filters;
},
handleSearch() {
this.startSearch({ type: this.selected_type, filters: this.filters });
},
...mapActions("search", {
startSearch: "startSearch"
})
},
computed: {
...mapGetters("search", {
results: "results"
})
}
</script>
My question is: why the item in the for loop (in the template section) always return undefined ?
Thank you very much for your answers.
So far, I found it:
in computed should be an array, not an object so:
...mapGetters("search", [
"results"
]
// Now results is populated.

How could I get changed value from vuetify select by emitting in atomic design pattern?

I am trying to get the changed value of vuetify v-select by using $emit but it doesn't work.
I divided components by applying atomic design pattern (atoms(child component and not to connect with the store), organisms(parent component)) and vuex stores.
I think $emit data is OK but anything doesn't work after the process.
This is for a new application for management page with
using vue, vuex, vuetify, atomic design connecting to API server.
Components
child component - in atoms folder
<template>
<v-select
:items="list"
:label="label"
v-model="selected"
item-value="id"
item-text="name"
return-object
#change="changeSelected"
></v-select>
</template>
<script>
export default {
props: ["list", "label", "defaultSelected"],
data() {
return {
selected: this.defaultSelected
};
},
methods: {
changeSelected(newValue) {
console.log(newValue); // display changed new data
this.$emit("changeSelected", newValue);
}
}
};
</script>
parent component - in organisms folder
<template>
<v-select-child
:select-label="label"
:select-list="list"
:default-selected="selected"
#change-selected="changeSelected" // problem issue?
>
</v-select-child>
</template>
<script>
import { mapState } from "vuex";
export default {
data() {
...
},
computed: {
...mapState({
list: state => state.list
})
},
methods: {
changeSelected() {
console.log("changeSelected"); // doesn't work
this.$store.dispatch("setSelected", { payload: this.selected });
}
}
};
</script>
vuex stores
index.js
export default new Vuex.Store({
modules: {
xxx
},
state: {
list: [
{
name: "aaaaa",
id: "001"
},
{
name: "bbbbb",
id: "002"
}
]
},
getters: {},
mutations: {},
actions: {}
});
xxx.js
export default {
selected: { id: "001" }
},
getters: {
//
},
mutations: {
updateSelected(state, payload) {
console.log("payload"); // doesn't work
console.log(payload);
state.selected = payload;
console.log(state.selected);
}
},
actions: {
setSelected({ commit }, payload) {
console.log("Action"); // doesn't work
commit("updateSelected", payload);
}
}
};
It does not print any console log after changeSelected function.
In document
Unlike components and props, event names don’t provide any automatic
case transformation. Instead, the name of an emitted event must
exactly match the name used to listen to that event.
That means if you emit event like $emit('changeSelected'), then you need to use #changeSelected. #change-selected will not work.
<v-select-child
:select-label="label"
:select-list="list"
:default-selected="selected"
#changeSelected="changeSelected"
>
</v-select-child>
I found a solution below:
child component
<template>
<v-select
:label="label"
:items="list"
v-model="selected"
item-value="id"
item-text="name"
return-object
></v-select>
</template>
<script>
export default {
props: ["list", "label", "defaultSelected"],
computed: {
selected: {
get() {
return this.defaultSelected;
},
set(newVal) {
if (this.selected !== newVal) {
this.$emit("changeSelected", newVal);
}
}
}
}
};
</script>
parent component
<template>
<v-select-child
:label="label"
:list="list"
:defaultSelected="selected"
#changeSelected="changeSelected" // fix the property using camelCase
></v-select-child>
</template>
<script>
import { mapState } from "vuex";
export default {
data() {
...
},
computed: {
...mapState({
list: state => state.list
})
},
methods: {
changeSelected(val) { // val: changed object value
this.$store.dispatch("setSelected", { id:val.id });
}
}
};
</script>
You can also use watch;
<v-select
:label="label"
:items="list"
v-model="selected"
item-value="id"
item-text="name"
></v-select>
</template>
...
watch:{
selected(){
this.$emit('changeValue', this.selected.id');
}
}
...
and from parent;
<child #changeValue="id = $event" .. />