![](/img/trans.png)
[英]Is Context.Provider and Context.Consumer the same as document.querySelector() in JavaScript?
[英]How to create a `context.Provider`/`context.Consumer`-like structure to pass values in a bot app?
我正在尝试将一个对象数组的第一个位置内的属性传递给另一个模块,以便以后可以使用该值。 我试图将其作为module(args)传递,但它一直在读取默认值0。有没有办法做到这一点?
我试图实现一些React.context
但是Bot框架仿真器拒绝了它。
/////////////////Module that ll acquire the value/////////////////////////////
getCard(bot, builder, params) {
let configValues = { ...params[0] }
bot.dialog(`${configValues.path}`, function (session) {
var msg = new builder.Message(session);
const cardItem = (obj) => {
return (new builder.HeroCard(session)
.title(`${obj.title}`)
.text(`R$ ${obj.price}`)
.images([builder.CardImage.create(session, `${obj.img}`)])
.buttons([
builder.CardAction.imBack(session, `${obj.price} Item adicionado!`, 'add to cart')
// !onClick event must add the current obj.price to
// the configValues.total(Ex: configValues.total += obj.price)!
])
)
}
msg.attachmentLayout(builder.AttachmentLayout.carousel)
msg.attachments(
eval(params.map(obj => cardItem(obj)))
);
//!in here before end the dialog is where i want to update
// the configValues.total so i can show it in the -> Checkout module
session.send(msg).endDialog()
}).triggerAction({ matches: configValues.regex });
}
}
//////////////CheckOut.Module///////////////////////////////
{...}
let configValues = { ...params[0] }
let state = {
nome: "",
endereco: "",
pagamento: "",
total: configValues.total // this is the value to be read
}
bot.dialog('/intent', [
{...},
(session, results) => {
state.pagamento = results.response
session.send(
JSON.stringify(state) // here is the place to be printed
)
{...}
]
).triggerAction({ matches: /^(finalizar|checar|encerrar|confirmar pedido|terminar)/i })
由于您已解决了原始问题,因此我将在您的评论中回答。
您的问题在这里:
cartId.map((obj, i , arr) => {
// if (!obj.total) {
// obj.total.reduce(i => i += i)
// }
const newtotal = new total
newtotal.getTotals(bot, builder, obj, arr)
})
cartId
包含每个项目的总计。 当您在其上调用map
时,您将每个项目分别传递给getTotals
,后者将每个项目传递给checkout()
您不能将所有总数相加,而只能将一项总数相加的原因是,您将cartId
传递给checkout
而cartId
已更改为仅一项。 相反,您可以执行几项不同的操作:
从cartItems
传递整个cartId
,并在totalConstructor()
和checkoutConstructor()
使用诸如for (var key in cartItems)
的totalConstructor()
。 这可能是最简单的,但不是很高效的内存。
使用BotBuilder的状态存储将totals
数组存储在userData
,然后对其求和。 这可能更难以实现,但是将是一条更好的选择。 这是一个可以帮助您入门的示例 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.