首页 > 解决方案 > 将 didOpen sweetalert2 参数中的值传递给 .then

问题描述

我已经构建并实现了一个带有值的 JSON 对象的 didOpen 参数。

问题是......如何将该 JSON 对象传递给 Swal.fire({ }) .then 箭头函数的 then 部分

Swal.fire({
  html: ` HERE IS ALL THE ID's STUFF `,
  didOpen() {
    let itemPOST = {
      /* the json i trying to catch */ }

    // do bunch of stuff and save it into itemPOST

  } //==>end of didOpen
}).then(() => {
  //console log here for itemPOST
})

标签: javascriptsweetalert2

解决方案


解决方案是在 swal 之后包含一些变量,将值传递给该变量并在 .then() 箭头函数中调用它

let giveMeTheValue;  //<==    variable to receive the value
Swal.fire({
  html: ` HERE IS ALL THE ID's STUFF `,
  didOpen() {
    let itemPOST = { /* the json with data */ }

    // do bunch of stuff and save it into itemPOST

    giveMeTheValue = itemPOST   //<== giving the data to pass to then()
  } 
}).then(() => {
  console.log(giveMeTheValue)
})

推荐阅读