R
R
Rufix2020-11-27 16:58:57
Vue.js
Rufix, 2020-11-27 16:58:57

How to bind data received from store?

Hello, I have such a task: I am making a page with profile settings.
There is a "Profile settings" block in which there are inputs, for example, let's take the "First name" and "Last name" fields.
In the store, I have an object that contains all the information I need about the authorized user.
I need to take this information from the store, create the corresponding fields in data, for example, firstName and lastName for binding with inputs.
The difficulty lies in displaying the current user data in these fields, while making a binding.

What I have:
- get the currentUser() in the computed properties by calling this.$store.getters.getCurrentUser
- a data object that has 2 fields created: firstName: "" and lastName: ""
- 2 inputs with v-model="firstName" and "lastName" respectively

Tried to set the value of this.currentUser.first_name from computed in data, but we don't have access to computed properties from data.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey Vasilev, 2020-11-27
@Nolis

use get and set in computed st.
I think this will be what you need

computed: {
  fullName: {
    // геттер:
    get: function () {
      return this.firstName + ' ' + this.lastName
    },
    // сеттер:
    set: function (newValue) {
      var names = newValue.split(' ')
      this.firstName = names[0]
      this.lastName = names[names.length - 1]
    }
  }
}

G
Gimir, 2020-11-27
@Gimir

Write an assignment to mounted(), and everything will be fine

A
Alexey Yarkov, 2020-11-27
@yarkov Vue.js

mounted() {
    this.userName = this.currentUser.userName;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question