const 后的意外令牌 [英] Unexpected Token after const

查看:53
本文介绍了const 后的意外令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试指定一个常量时,我​​在 React 中遇到了意外的令牌错误,我似乎无法弄清楚原因.

I am getting an unexpected token error in React when I try to specify a constant, and I cannot seem to figure out why.

我的代码非常简单,我已经按照 react-bootstrap 示例 here 几乎完全一样.

My code is pretty simple, and I have followed the react-bootstrap examples here almost exactly.

我的代码如下:

import { Component, PropTypes } from 'react';

var rbs = require('react-bootstrap'),
Panel = rbs.Panel;

export default class ResumeSection extends Component {
  constructor(...args) {
    super(...args);
    this.state = {
      open: true
    };
  }

  const title = (
    <h3>Panel title</h3>
  );

  render() {
    return (
      <Panel collapsible expanded={this.state.open}>
        <p>Body</p>
      </Panel>
    );
  }
}

错误发生在 title 紧跟 const 之后,只是说 SyntaxError: Unexpected Token

The error occurs on title directly after const and just says SyntaxError: Unexpected Token

推荐答案

你不能像那样在类体中定义一个常量;它应该被移动到一个方法中.

You can't define a const in the class body like that; it should be moved into a method.

render() {
  const title = (
    <h3>Panel title</h3>
  );
  // ...
}

这篇关于const 后的意外令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆