我的客户要求我们的聊天机器人支持IE11。我必须修改webchat代码,使其不能使用es6的功能,比如箭头功能,因为它们在IE11es5中不支持。在大多数情况下,我是成功的,但有一件事我没能得到工作,就是发送我正在发送的消息事件。新的代码在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控制台说一个标识符预期和整个webchat无法加载。在Chrome浏览器中工作正常。如果我改变 function({dispatch})
到只是 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: ''
}
});
你可以重命名参数以减少冗余,但这是不需要的。
你发布的代码的问题是你从来没有 return next(action)
. 因此,当它流经以下地方时,流量永远不会前进。store
. 增加这一行,网络聊天就会像预期的那样每3秒发送一次。您可以在 05.custom-componentsb.send-typing-indicator(自定义组件)。 网络聊天样本,如有需要。
请注意。
PageTitleNotification
因为它是未定义的。interval
你必须在你的代码中的其他地方定义,而不是提供。webchat-es5.js
CDN,它的后盾是在IE上运行的polyfills。希望对大家有所帮助!
var interval;
const store = window.WebChat.createStore({}, function(dispatch) {
return function(next) {
return function(action) {
console.log(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') {
// 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.dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: {
name: 'inactive',
value: ''
}
});
}, 3000)
}
return next( action )
}
}
} );
开发者控制台。
Bot日志: