1

我的客户要求我们的聊天机器人支持 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),机器人会呈现,但发送非活动消息到机器人(如下)的调度语句不再在 IEChrome 中起作用。

// Notify bot the user has been inactive
dispatch({
    type: 'WEB_CHAT/SEND_EVENT',
    payload: {
        name: 'inactive',
        value: ''
    }
});

为什么 IE 无法识别{dispatch}为标识符,我该怎么做才能使其正常工作?

4

2 回答 2

4

IE11 不支持该语法。ES6 允许通过变量名设置对象属性,所以 ES5 等价物是:

window.WebChat.createStore({}, { dispatch: dispatch }) {
  // ...
}

编辑:

经过一番调试,最终的解决方案是直接将dispatch对象传递给createStore函数,并访问其dispatch方法:

dispatch.dispatch({
    type: 'WEB_CHAT/SEND_EVENT',
    payload: {
        name: 'inactive',
        value: ''
    }
});

您可以重命名参数以减少冗余,但这不是必需的。

于 2020-05-05T17:48:28.547 回答
2

您发布的代码的问题是您永远不会return next(action). 因此,当流通过store. 添加这一行,网络聊天将按预期每 3 秒发送一次。如果需要,您可以参考05.custom-components/b.send-typing-indicator网络聊天示例中的代码。

请注意:

  • 我注释掉了,PageTitleNotification因为它是未定义的。
  • 我确实interval必须在未提供的代码中定义您必须在其他地方定义的内容。
  • 确保您使用的webchat-es5.js是支持 polyfill 的 CDN,以便在 IE 上运行。

希望有帮助!

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 )
    }
  }
} );

开发者控制台:

在此处输入图像描述

机器人日志记录:

在此处输入图像描述

于 2020-05-07T16:59:00.043 回答