[英]Variable becomes undefined after passing it as an argument (vue js to js)
I have the following functions:我有以下功能:
view.vue视图.vue
await this.newShelf(payload, token)
shelves.js货架.js
async newShelf({ commit }, payload, token) {
But when I try to use token in newShelf it becomes undefined for some reason but if I print it in the vue file it just prints the value of token.但是当我尝试在 newShelf 中使用令牌时,由于某种原因它变得未定义,但是如果我在 vue 文件中打印它,它只会打印令牌的值。 How can I make this work?
我怎样才能使这项工作?
payload is being deconstructed into { commit }有效载荷被解构为{ commit }
await this.newShelf(objectWithCommit, payload, token)
//or
async newShelf(payload, token) {}
//or
async newShelf({ commit }, token) {}
You need to remove one of these (I don't know what var are you using)您需要删除其中之一(我不知道您使用的是什么 var)
commit - Mutation commit
State - State Variable
Payload - Request Parameter
We can pass token as payload parameter我们可以将令牌作为有效负载参数传递
async newShelf({commit, state}, payload) {
}
You can pass the payload as an object:您可以将有效负载作为 object 传递:
let payloadObject = {
payload: paylaod,
token: token
}
then use it like this然后像这样使用它
await this.newShelf(payloadObject)
async newShelf({ commit }, payload) {
payload.payload // your payload
payload.token // your token
}
You can pass it like this你可以这样通过
await this.newShelf({otherData, token})
and can access it like this并且可以像这样访问它
async newShelf({ commit }, obj) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.