简体   繁体   English

使用vue.js存储对象的元素

[英]Store the elements of an object using vue.js

    data() {
        return: {
          user: {},
          userName: "",
          userAge: ""
    }
  },

methods: {

    saveUserName: function() {
          this.userName = this.newUserName;
          this.$refs.userNameModal.hideModal();
          this.$session.set('userDetails', {userName: this.userName});
        },

    saveUserAge: function() {
          this.userAge = this.newUserAge;
          this.$refs.userAgeModal.hideModal();
          this.$session.set('userDetails', {userAge: this.userAge});
        },

    },

    beforeMount: function() {
    if (this.$session.exists('userDetails')) {

          this.user = this.$session.get('userDetails');
          console.log("userDetails", this.user)
        }

    }

I have a user form, which for every entry, a Modal pops up, and the user is required to fill in, and by pressing a done button, the modal closes, calls a specific function(ie saveUserName()), which the modal, and set it to session storage. 我有一个用户窗体,对于每个条目,都会弹出一个模态,并且需要用户填写,然后通过按下完成按钮,模态会关闭,并调用特定的函数(即saveUserName()),该模态,并将其设置为会话存储。 So I am wondering how could I populate the user object time by time, and setting these values into my session storage?? 所以我想知道如何按时间填充用户对象,并将这些值设置到会话存储中? As at the moment, the first value gets reset, and the second one takes over, and in my case, my user object has either userName or userAge. 到目前为止,第一个值被重置,第二个值被接管,在我的情况下,我的用户对象具有userName或userAge。 What I am expecting instead (in the case that my session storage is not empty) from the log beforeMount is user: {userName: "Mario", userAge:27} Thanks 我所期望的(在我的会话存储不为空的情况下)来自日志beforeMount是用户:{userName:“ Mario”,userAge:27}谢谢

data() {
  return {
    user: {
      userName: "",
      userAge: ""
    },
  };
},

methods: {
  saveUserName: function() {
    this.user.userName = this.newUserName;
    this.$refs.userNameModal.hideModal();
    this.saveUser();
  },

  saveUserAge: function() {
    this.user.userAge = this.newUserAge;
    this.$refs.userAgeModal.hideModal();
    this.saveUser();
  },

  saveUser: function() {
    this.$session.set('userDetails', this.user);
  },
},

beforeMount: function() {
  if (this.$session.exists('userDetails')) {
    this.user = this.$session.get('userDetails');
    console.log("userDetails", this.user)
  }
}

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

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