Updating values in Vuex stores

Currently, I am delving into Vuex and facing a roadblock in my endeavor to have the number value change each time the p-element is clicked. The mutation responsible for this is called changeNumber. Additionally, I aim to display the updated number value within the same p-element. Although I have made some progress with the code below, I find myself stuck:

let object = {

}

let state = {
  object,
  someArray: [],
  someBool: true,
  counter: 0,
  someOtherValue: 'Text'
}

let changeNumber = {
  increment(state) {
    state.counter += 1
  }
}

let store = new Vuex.Store({
  changeNumber,
  state
})

Vue.component('counter-button', {
  computed: {
    counter() {
      return this.$store.state.counter
    }
  },
  template: `<input :value="$store.state.counter" @click="$store.commit('increment')" type="button">`
})

Vue.component('some-component', {
  computed: {
    someOtherValue() {
      return this.$store.state.someOtherValue
    }
  },
  template: '<div>{{ someOtherValue }}</div>'
})

new Vue({
  el: '#app',
  store,
  template: '<some-component></some-component>',
})

Answer №1

When creating a new Vuex store using new Vuex.Store({options}), it is important to note that you cannot include the function changeNumber as an option.

Refer to the documentation for more details - the function should be placed within the mutations section instead.

By replacing changeNumber with mutations, your solution will be functional. See example below:

const mutations = {
  increment(state) {
    state.counter += 1
  }
}

const store = new Vuex.Store({
  mutations,
  state
})

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

Looking to create a GitHub example in Fiddle but running into issues with getting the result?

I've been working on an example on Fiddle, but it's not functioning as expected. I want to implement i18next so that the text changes when the user switches languages. After searching for a solution, I came across this GitHub repository: https:// ...

What is the best way to pass the text of a selected option in a dropdown menu to a function when a button is clicked in AngularJS?

Alright, so I've got this HTML page with a select element. The select has three options defined in the $scope below: <div id="mainID" ng-controller="theController"> <form name="assetTypeForm" class="form-horizontal" role="form" novalidat ...

Is it possible to determine if an 'any' item aligns with a union type?

I have an object (of type 'any') from library A that I need to pass to a function in library B. This function is expecting a Type B-Input, which is a Union Type (number | string | somethingCustom). How can I verify if the object is compatible? S ...

Utilize Moment in React-Native to adjust the time zone format for the Vi Locale

I'm encountering a date formatting issue while using Moment in React-Native. Specifically, I have formatted the date to an English locale, but now I want it to also support the Vietnamese locale: Moment('2022-09-02T02:00:00+00:00') . ...

The cookie isn't being established in the browser through the backend, rather it is included in the request itself

I'm encountering an issue where the cookie does not attach to the browser after a successful request. Despite showing up successfully in the response, I have tried multiple solutions and configurations without success. The most promising solutions for ...

Strategies for transferring a JavaScript variable from a JSP to a servlet

For my reporting module, I am utilizing the google visualization API java wrapper along with image charts. To pass the url of the generated chart to a servelet, I am using the getImageUrl() method to retrieve the url and storing it in a javascript variabl ...

Creating test doubles using Sinon allows you to effectively block the execution of the original function

I am currently engaged in JavaScript unit testing using Mocha and Sinon. My goal is to verify if a specific method is called under certain conditions. However, I have encountered difficulties purely testing the method call. To clarify, I aim to replace th ...

How can I implement Jquery to make my page scroll at a slower pace when a button is clicked?

I am a beginner when it comes to JQuery and I have been able to make my page scroll to the top and bottom but I would like to slow down the movement. How can I adjust the scroll speed? document.getElementById('sur2').onclick = function () { ...

Using Material-UI version 1, pass the outer index to the MenuItem component when clicked

Within my component, there is a Table that displays rows generated from a custom array of objects. In the last TableCell, I aim to include an icon button that, upon being clicked, opens a Menu containing various MenuItem actions (such as edit and delete). ...

What is the best way to transfer information to a different component using Vue router?

At the moment, I have a component that is displayed on the page. When I check the this.$router variable inside this component, I notice that the full path property is '/hello/reports?report=3849534583957' and the path property is '/hello/rep ...

Tips for ensuring your jQuery events always trigger reliably: [Issues with hover callback not being fired]

My background image animation relies on the hover callback to return to its original state. However, when I quickly move the mouse over the links, the hovered state sticks. I suspect that I am moving the mouse off before the first animation completes, caus ...

How can I add color to arrow icons in tabulator group rows?

Is there a way to specify the color of the arrows in collapsed/expanded group rows? Also, what CSS code should I use to define the font color for column group summaries? You can view an example of what I am trying to customize here: https://jsfiddle.net/s ...

In order for the JavaScript function to properly execute, it requires the page to be

Check out this code snippet: <script> function scaleDown() { $('.work > figure').removeClass('current').addClass('not-current'); $('nav > ul > li').removeClass('current-li'); ...

Display a button that allows the header to slide down after sliding

Currently building my website and encountering an issue. At the top of the page is my header with a navigation menu inside it. As I scroll down the page and the header moves out of view, I would like another DIV to slide down from the top (fixed in positi ...

Exploring the concept of inheritance in JavaScript and Angular programming

Currently, I am working on a project called "hello world" and it involves two HTML pages named "configuration.html" and "add configuration.html". Each page has its own controller defined as follows: angular.module('MissionControlApp').controller ...

Optimizing jqGrid: Enhancing saveRow function to properly synchronize with editRow function

Exploring how to customize jqGrid's add function for my own needs. I have a navButton with specific requirements: When the user clicks the button, a new row in edit mode should appear on the grid. Once the user enters data and presses enter, the dat ...

Updating form validation by altering input value

I'm currently utilizing JavaScript regular expressions for form validation in my project. After successfully completing the validation without any errors, upon clicking the submit button, I want the form to be submitted and change the submit value to ...

Converting array elements to strings when extracting in JSON with JavaScript

I am working with a Json document that looks like this: { "_id": "13fee4aad95c7f822c0b559bd8d09fb0", "_rev": "5-336dea3680af3e7ec0de29369be90b09", "attributeCollection": { "attributeArray": [ { "name": "Web Issue", "val ...

Confirm the data in each field of a form individually

I am facing an issue with a form that contains 7 fields. When I submit the form to the "register.php" page, it processes each field one by one and outputs the result as a whole. However, I need to validate the data using ajax, collect the output one by one ...

Argument not accepted in JavaScript

Currently, I am encountering a unique error message while working with a complex function that iterates through elements in my JavaScript onSuccess event. The error I am seeing is: exception encountered : {"message": "Invalid argument.", "description": " ...