【发布时间】:2019-02-14 09:24:57
【问题描述】:
尝试访问 RecipeList.js 和 Recipe.js 的 .props 时出现语法错误。
这里是 Recipe.js 的代码示例:
import React, {Component} from 'react';
import "./Recipe.css";
class Recipe extends Component {
// props: any; uncommenting this will fix the bug
render() {
// don't have to use return and parentheses for arrow with JSX
const ingredients = this.props.ingredients.map((ing, ind) => (
<li key={ind}>{ing}</li>
));
const {title, img, instructions} = this.props
return (
<div className="recipe-card">
<div className="recipe-card-img">
<img src={img} alt={title}/>
</div>
<div className="recipe-card-content">
<h3 className="recipe-title">
{title}
</h3>
<h4>
Ingredients:
</h4>
<ul>
{ingredients}
</ul>
<h4>
Instructions:
</h4>
<p>
{instructions}
</p>
</div>
</div>
)
}
}
但是,该项目没有引发编译时错误,并且网站运行良好。
Screenshot of app working fine with no chrome console or terminal errors
我认为这与我的代码关系不大,而与 TypeScript 或某种带有 Javascript 的 VScode 预设配置的关系更是如此,因为我在构建 @ 时遇到了类似的问题,因此无法识别每个组件的 .props 属性987654323@ 到我的编辑器中(我什至从网站复制粘贴了最终的 index.js 代码以确保),尽管应用程序运行良好,没有编译时错误。
Screenshot of the same .prop errors after following React Tutorial
解决这个问题的唯一方法是,如果我真的硬编码并为每个类创建一个 props 属性并将其设置为任何类似的属性:
Screenshot of only solution to the syntax error
这是我更新的依赖项
"dependencies": {
"@types/react": "^16.4.13",
"prop-types": "^15.6.2",
"react": "^16.5.0",
"react-dom": "^16.5.0",
"react-scripts": "1.1.5",
"typescript": "^3.0.3"
}
【问题讨论】:
-
请Property 'props' does not exist on type 'Home' 或TypeScript Property 'props' does not exist 帮忙?他们推荐
@types/react库。 -
@SamVK 不幸的是,当我添加@types/react 库时,我收到类似的错误“属性'recipes'不存在于类型'Readonly & Readonly'。”
标签: javascript reactjs typescript visual-studio-code babeljs