What is preventing the input value from being populated with a value from vuex?

To ensure that the value entered in the input is saved into Vuex and then stored in localstorage, follow these steps. If you close the application and reopen it, the value from localstorage should be returned to the input field. However, if your input value is not being saved, there could be an issue with the code. Please review the following and let me know what needs correction:

Component

<f7-list-input
  placeholder="Username"
  type="text"
  v-bind:value="name"
  @input="onPersist"
/>

export default {
mounted() {
  if (localStorage.name) {
    this.name = localStorage.getItem('name');
        }
    },

computed:{
    name(){
        return this.$store.state.name;
    }
},
methods:{
    onPersist(event){
        this.$store.commit('persist', event.target.value);
    }
}
    };
    </script>

VUEX store

export default new Vuex.Store({
    state: {
        name: ''
    },
    mutations: {
        persist(state,payload){
        state.name = payload; 
        localStorage.setItem('name', state.name);
       },
    }
});

Answer №1

It seems like you are attempting to assign a value to a computed property that does not have a setter. This means that the line of code below will not result in any reactive change or state mutation within the Vuex store:

this.name = localStorage.getItem('name');

If you want to trigger a change and update the state in the Vuex store, I recommend reading up on form handling in Vuex for a better understanding.

To address this issue, you simply need to call the 'persist' mutation in your mounted hook with the data fetched from local storage.

<f7-list-input
placeholder="Username"
type="text"
:value="name"
@input="onPersist"/>

export default {
 mounted() {
  if (localStorage.name) {
    const existingName = localStorage.getItem('name');
    this.$store.commit('persist', existingName);
  }
 },

 computed:{
   name(){
    return this.$store.state.name;
   }
 },

 methods:{
  onPersist(){
    this.$store.commit('persist',event.target.value);
  }
 }
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular sorting data is not functioning as expected

I've been attempting to utilize AngularJS to organize my data, but unfortunately, it seems to be ineffective. I am fetching data from Firebase () and using Node.js to transmit it to a controller. Controller Code var myApp = angular.module('myA ...

Remove Vue Component from the styling of current application

We integrated a Vue component (using Vuetify) into our existing .NET MVC application. The issue we are facing is that the Vue component is inheriting all the CSS styles from the rest of the application. Here's a simplified version of the HTML structur ...

Internet Explorer 11 encountering a syntax error with SweetAlert2

I have implemented the following code from the SweetAlert2 examples page: swal({ title: 'Are you sure?', text: "You won't be able to revert this!", type: 'warning', showCancelButton: true, confirmButtonColor: '#3085 ...

Examining iOS devices using Chrome

Here are some questions I have: I am currently using Google Chrome's JavaScript profiling to optimize my application's performance. However, I need to do this for an iOS device and I'm not sure how to do it from Chrome. Can you pro ...

Issue with Prettier AutoFormatting in a project that combines TypeScript and JavaScript codebases

Recently, I've started incorporating TypeScript into an existing JavaScript project. The project is quite large, so I've decided to transition it to TypeScript gradually. Below is a snippet from my eslintrc.js file: module.exports = { parser: ...

HTML and CSS: Understanding DataTables Header Padding

http://jsfiddle.net/d1zqsayh/ (Strange issue not appearing on jsfiddle, very odd) When I modify the padding in the .css file at line 41 from 10px 18px; to 10px 17px;, I notice a difference in how the content is displayed on my screen in both Google Chrome ...

Error in Asp.Net Mvc Bundle: Incorrect Path for Scripts

In my ASP. NET MVC Project, I have set up BundleConfig to include jquery, bootstrap, and modernizr scripts. Everything works well when running locally, with the path leading to the exact .js file. However, when I publish the project to the hosting server, ...

Has the user successfully authenticated with Google?

Possible Repetition: How to verify if a user is logged into their Google account using API? I'm working on a website that displays a Google Calendar. My query is: Can I determine whether a user is currently logged into a Google Account? If it&ap ...

Tips for initializing and updating a string array using the useState hook in TypeScript:1. Begin by importing the useState hook from the

Currently, I am working on a React project that involves implementing a multi-select function for avatars. The goal is to allow users to select and deselect multiple avatars simultaneously. Here is what I have so far: export interface IStoreRecommendation ...

When the page is loaded, trigger the controller function using AngularJS methodology

Is there a specific way in AngularJS to call a controller function inside the view on page load, or should I follow the classic syntax used in ASP.NET MVC Razor views (such as using ajax)? ...

JavaScript code not functioning properly to submit form

My journey with my first web application has hit a roadblock. I am attempting to use javascript to submit a form by clicking on text, but nothing happens when I click. All I want is for it to open a simple webpage at this stage. Although I have experience ...

Creating DIV's with Equal Heights Using AngularJS ng-repeat

I'm currently facing an issue with aligning two side-by-side divs to the same height when the content is generated using an ng-repeat function. Using flexbox is causing responsiveness issues, and I'm unsure of the appropriate time to call a jQuer ...

Cross domain tracking pixel implemented using ASP.NET, jQuery, and AJAX technology

Currently, I have a page tracking system that uses $.post(PAIRS-DATA) in JavaScript to send collected information back to the server and load as a tracking pixel. finally { //tracking pixel Response.ContentType = "i ...

Why isn't the DIV I created in JavaScript appearing when the page loads?

I've been struggling to get the DIV generated by the Javascript code, particularly in the function parameter id within creatediv('xdiv', html, 100, 100, 10, 10) where id = 'xdiv'. <!DOCTYPE html> <html> <head> &l ...

Update the message displayed in the user interface after the view has been fully rendered in an Express application, following the execution of asynchronous

I have created a simple express app that reads files from a directory, renames them, zips the files asynchronously, and then renders another view. The file reading and renaming are done synchronously, while the zipping part is handled asynchronously. My cu ...

"Internet Explorer text input detecting a keyboard event triggered by the user typing in a

It appears that the onkeyup event is not triggered in IE8/IE9 (uncertain about 10) when the enter button is pressed in an input box, if a button element is present on the page. <html> <head> <script> function onku(id, e) { var keyC = ...

Is there a way to deactivate the toggle button in my code?

<label class="switch switch-yes-no" id="disable_'+id+'" style="margin: 0;font-weight: bold;"> <input class="switch-input" type="checkbox" id="disable_'+id+'" /> <span class="switch-label" data-on="Enabled" data-off="Disab ...

Searching for documents in MongoDB that meet specific criteria has become possible through the use

Criteria: COUNT the total number of documents in the collection WHERE objects.objectType is 'group' AND (objects.objectType is NOT 'person' AND relation is 'Exposed_to') Expectation: should return the count of all documents W ...

Function in Typescript used for checking types and catching compilation errors from external sources

Fact: I am currently using TS version 2.3.4. I have developed a function that checks if a variable is defined (it takes the parameter variable and returns 'undefined' !== typeof variable). It's quite simple. export function IsDefined(variab ...

Send the user back to the previous page once authentication is complete

I am integrating Google authentication through Passport in my web application and I am facing an issue with redirecting the user back to the original page they requested after a successful sign-in. It seems like the use of location.reload() might be causin ...