ReactJS - 语法错误

时间:2018-03-14 08:41:52

标签: javascript reactjs

我是Javascript的新手,我在解决这个问题上遇到了麻烦。当我的'params'被声明为var时,我被抛出“[js]意外的令牌。预计会有构造函数,方法,访问器或属性。”错误。错误的屏幕截图 - http://i65.tinypic.com/35ist1s.jpg

但是,当我删除var变量时,我会抛出多个错误(例如“; expected”和“期望的属性赋值”。错误的屏幕截图 - http://i64.tinypic.com/21c6vwj.jpg

这是我的代码:

import React from 'react';
import * as ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import Backend from '../Backend';
import * as BotChat from './components/botchat.js'
import styles from './styles/botchat.css';
require("babel-register");

class Chat extends React.Component{
  var params = BotChat.queryParams(location.search); //var is identified as unexpected token
  window.botchatDebug = params.debug && params.debug === 'true';

ReactDOM.render(
<BotChat.App

    user={{
      id: params.userid || 'userid',
      name: params.username || 'username'
    }}

    bot={{
      id: params.botid || 'botid',
      name: params.botname || 'botname'
    }}

    avatar={{
      showTheirAvatar: showbot || false,
      showMyAvatar: showuser || false,
      initials: params['initials'] || 'TA',
      style: params['style'] || 'img',
      colorIndex: params['color'] || 0
    }}

    directLine={{
      secret: 'mySecretKey',
      webSocket: params.webSocket && params.webSocket === 'true' // defaults to true
    }}

    locale={params.locale}
    resize="detect"

      avatarOptions = {{
        showTheirAvatar: avatar['showTheirAvatar'],
        showMyAvatar: avatar['showMyAvatar'],
        avatarStyle: avatar['style'], // img, firstInitial, or bothInitials
        userInitials: avatar['initials'],
        userImgUrl: 'myLogo.png',
        paletteIndex: avatar['colorIndex'],
        avatarPalette: [
          "#721481",
          "#000000",
          "#767676",
          "#E81123",
          "#DA3B01",
          "#CA5010",
          "#10893E",
          "#038387",
          "#0078d7",
          "#9a0089",
          "#bf0077"
        ]
      }}
    /> );
  }

Chat.defaultProps = {
  name: 'John Smith',
};

Chat.propTypes = {
  name: PropTypes.string,
};

export default Chat;

非常感谢任何帮助!谢谢!

1 个答案:

答案 0 :(得分:0)

更改您的类声明语法。阅读一些指南如何使用类。你不能在构造函数或方法之外声明变量和工作逻辑。

{{1}}