简体   繁体   English

在我的情况下,我想推送输入值并将其存储在本地存储 VUEX 的数组中。 我试过这个,但它不工作

[英]In my situation I want to push input value and store it in array in local storage VUEX. I've tried this but it's not working

In my situation I want to push input value and store it in array in local storage VUEX.在我的情况下,我想推送输入值并将其存储在本地存储 VUEX 的数组中。 I create an array in state.我在 state 中创建了一个数组。 First I push my value and then I stringify to set that value to the store in local storage.首先我推送我的值,然后我将该值设置为本地存储中的存储。 I get it in my array by parsing my JSON but it is not working.我通过解析我的 JSON 将它放入我的数组中,但它不起作用。 I don't know to where my value goes.我不知道我的价值去哪儿了。 Also console gives an error 'push is not a function'.控制台也给出错误“推送不是功能”。

    state:{
    formItems:JSON.parse(localStorage.getItem('formItems'))||[]
},
mutations:{
    formFunction(state, myInput){
        state.formItems.push({myInput});
        localStorage.setItem('formItems', JSON.stringify(state.formItems))
    },


<template>
<div>
    <div class="container">
        <div class="row">
            <div class="col s12">
                <input type="text" v-model="Input">
                <br>
                <button class="btn waves-effect" @click="saveInput">Save</button>
            </div>
        </div>
        <p>{{text}}</p>
    </div>
</div>
</template>
<script>

export default {
    name:"test",
    data:function(){
        return{
            Input:"",
        }
    },
    methods:{
        saveInput(){
            this.$store.commit('formFunction', this.Input);
            this.Input="";
        }
    },
computed:{
    text(){
        return this.$store.state.formItems;
    },
</script>

Solved.解决了。 I just found out that all local storage in browser should be cleared.我刚刚发现浏览器中的所有本地存储都应该被清除。 After clearing it works fine without any problems.清除后它可以正常工作,没有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM