我刚刚创建了一个构造函数来为 JSON 文件创建新用户。
结构应该是这样的:
{
"users": {
"userName1": {
"salary": [
"1234"
]
},
"userName2": {
"salary": [
"4321"
]
}
}
}
我的代码如下所示:
export const userDataControllerMixin = {
data() {
return {
userObj: {},
};
},
methods: {
NewUser(user, salary) {
this.user = user;
this.salary = salary;
user = {
salary,
};
},
// GETTING INPUT FROM USERS DIALOGBOX
getInput(inputName, inputSalary) {
const userName = document.querySelector(inputName).value;
const userSalary = document.querySelector(inputSalary).value;
const userData = new this.NewUser(userName, userSalary);
console.log(userData);
},
我得到的结构是错误的,它看起来像这样:
NewUser {user: "asd ", salary: "123"}
最佳答案
当您使用单词this
时,它意味着当前的父亲,在您的情况下NewUser
要按照您想要的方式获取变量,您需要执行以下操作:
NewUser(user, salary) {
this[user] = {
'salary':salary
};
},
关于javascript - 在 vue.js 中创建一个包含数据的 obj 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56959516/