Javascript 语法错误:意外标记,应为“quot;”&引用;

Javascript 语法错误:意外标记,应为“quot;”&引用;,javascript,reactjs,Javascript,Reactjs,我在react中遇到以下语法错误: 3:28:16 PM: Syntax error: Unexpected token, expected ";" (1:10) 3:28:16 PM: > 1 | render() { 3:28:16 PM: | ^ 3:28:16 PM: 2 | if (this.props.post) { 3:28:16 PM: 3 | return ( 3:28:16 PM: 4 | <

我在react中遇到以下语法错误:

3:28:16 PM: Syntax error: Unexpected token, expected ";" (1:10)
3:28:16 PM: > 1 | render() {
3:28:16 PM:     |          ^
3:28:16 PM:   2 |     if (this.props.post) {
3:28:16 PM:   3 |       return (
3:28:16 PM:   4 |         <div className="article">
3:28:16 PM:语法错误:意外标记,应为“;”(1:10)
下午3:28:16:>1 |渲染(){
下午3:28:16:|^
下午3:28:16:2 |如果(这篇文章){
下午3:28:16:3 |返回(
下午3:28:16:4 |
我已经多次查看了每个标记和括号,看看是否都有它们的开始和结束标记/括号,我似乎找不到错误。可能是其他原因,但我的javascript不够好,无法发现错误

我也不明白错误信息,当它说“预期”时,它究竟意味着什么

以下是错误消息引用的代码:

render() {
    if (this.props.post) {
      return (
        <div className="article">
          <a href={"/blog/" + this.props.post.ID} className="blackLink">
            {this.props.post.featured_image ? (
              <img
                className="img-responsive webpic"
                alt="article header"
                src={this.props.post.featured_image}
              />
            ) : (
              ""
            )}
            <h1 className="text-center">{this.props.post.title}</h1>
            <div className="content">{excerpt}</div>
          </a>
          <Link to={"/blog/" + this.props.post.ID}>
            <button className="btn">Read More</button>
          </Link>
        </div>
      );
    } else {
      return null;
    }
  }
render(){
如果(此.props.post){
返回(
阅读更多
);
}否则{
返回null;
}
}

我想你忘了把函数放在render()之前。所以它应该是这样的:

function render() {
    if (this.props.post) {
      return (
        <div className="article">
          <a href={"/blog/" + this.props.post.ID} className="blackLink">
            {this.props.post.featured_image ? (
              <img
                className="img-responsive webpic"
                alt="article header"
                src={this.props.post.featured_image}
              />
            ) : (
              ""
            )}
            <h1 className="text-center">{this.props.post.title}</h1>
            <div className="content">{excerpt}</div>
          </a>
          <Link to={"/blog/" + this.props.post.ID}>
            <button className="btn">Read More</button>
          </Link>
        </div>
      );
    } else {
      return null;
    }
  }
function render(){
如果(此.props.post){
返回(
阅读更多
);
}否则{
返回null;
}
}

看起来问题实际上与“render(){”有关,您在下面的代码中没有显示该问题

“render”是您正在定义的函数吗?如果是,您需要关键字“function”。如果您不将关键字放在那里,它会认为您正在调用函数“render();”,因此需要该语句末尾的“;”

 function render() {
     if (this.props.post) {
          return (
            <div className="article">
              <a href={"/blog/" + this.props.post.ID} className="blackLink">
                {this.props.post.featured_image ? (
                  <img
                    className="img-responsive webpic"
                    alt="article header"
                    src={this.props.post.featured_image}
                  />
                ) : (
                  ""
                )}
                <h1 className="text-center">{this.props.post.title}</h1>
                <div className="content">{excerpt}</div>
              </a>
              <Link to={"/blog/" + this.props.post.ID}>
                <button className="btn">Read More</button>
              </Link>
            </div>
          );
        } else {
          return null;
        }
      }
function render(){
如果(此.props.post){
返回(
阅读更多
);
}否则{
返回null;
}
}

您可以共享完整的代码吗?在引发此错误的整个类中,大多数情况下都会出现问题。请确保关闭所有对括号从行号开始计算,这已经是完整的代码。因为这显然是一个react render方法,您不能将其放入脚本中,您需要导入react并编写一个适当的类。如这是在js文件的最开始,linter希望
render()
是一个函数调用,因此希望语句以分号结尾。下面是一个正确用法的示例:(注意
render()
TodoApp
类的一个方法)不,OP正在编写一个(react)类。
render
是一个方法,前面不应该有
函数
。我看到其他人评论说您正在编写react类。如果是这样,请共享完整的代码好吗?