javascript - 无法通过 IE11/es5 的 botframework-webchat 中的自定义存储/调度发送消息
问题描述
我的客户要求我们的聊天机器人支持 IE11。我不得不修改网络聊天代码以不使用箭头函数等 es6 功能,因为 IE11/es5 不支持它们。在大多数情况下,我是成功的,但我无法开始工作的一件事是发送我正在发送的消息事件。新代码在 Chrome 中有效,但我在 IE11 中收到预期标识符错误消息。以下是相关代码:
const store = window.WebChat.createStore({}, function({dispatch}) { return function(next) { return function(action) {
if (action.type === 'WEB_CHAT/SEND_MESSAGE') {
// Message sent by the user
PageTitleNotification.Off();
clearTimeout(interval);
} else if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY' && action.payload.activity.name !== "inactive") {
// Message sent by the bot
clearInterval(interval);
interval = setTimeout(function() {
// Change title to flash the page
PageTitleNotification.On('Are you still there?');
// Notify bot the user has been inactive
dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: {
name: 'inactive',
value: ''
}
});
}, 3000)
}
return next(action);
}}});
以前第一行看起来像
const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
问题来了function({dispatch})
。IE 控制台说需要一个标识符并且整个网络聊天无法加载。它在 Chrome 中运行良好。如果我更改function({dispatch})
为 just function(dispatch)
,机器人会呈现,但发送非活动消息到机器人(如下)的调度语句不再在 IE或Chrome 中起作用。
// Notify bot the user has been inactive
dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: {
name: 'inactive',
value: ''
}
});
为什么 IE 无法识别{dispatch}
为标识符,我该怎么做才能使其正常工作?
解决方案
IE11 不支持该语法。ES6 允许通过变量名设置对象属性,所以 ES5 等价物是:
window.WebChat.createStore({}, { dispatch: dispatch }) {
// ...
}
编辑:
经过一番调试,最终的解决方案是直接将dispatch
对象传递给createStore
函数,并访问其dispatch
方法:
dispatch.dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: {
name: 'inactive',
value: ''
}
});
您可以重命名参数以减少冗余,但这不是必需的。
推荐阅读
- python - Raspberry Pi 上的列表索引超出范围
- django - 使用 http 到 https 重定向和通配符子域将 www 重定向到非 www
- nginx - 为什么要在端口 80 上运行 Varnish 以进行仅 HTTPS 设置?
- javascript - 使用 semver 强制数组,对其进行排序,然后将其恢复为强制前对应的数组
- android - 需要解决我构建的应用程序问题的想法
- java - 在 Java 上重试请求的最优雅方式
- python - 如何让 print() 和 input() 中的文字一一出现?
- powershell - Powershell - 将多个磁盘驱动器连接到单独的变量中
- c# - C# WPF MVVM | 更改绑定到状态栏变量
- java - 为什么不重复三遍?