当前位置: 首页 > 知识库问答 >
问题:

属性“XYZ”在类型“Readonly”

巫马泰
2023-03-14

尝试访问时出现语法错误。这两个都是互惠主义者的道具。js和菜谱。js。

下面是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>
        )
    }
}

截图。道具误差

但是,该项目不会抛出html" target="_blank">编译时错误,并且网站运行良好。

应用程序截图工作正常,没有chrome控制台或终端错误

我认为这与我的代码关系不大,与TypeScript或某种带有Javascript的预设配置关系更大,VScode无法识别每个组件的. props属性,因为当我将React教程项目构建到我的编辑器中时,我也会遇到类似的问题(我甚至从网站复制粘贴了最终的index.js代码,以确保),尽管应用程序运行良好,没有编译时错误。

同样的截图。遵循React教程后的道具错误

解决这个问题的唯一方法是,如果我真的硬编码并为每个类创建一个props属性,并将其设置为任何类似的值:

语法错误的唯一解决方案截图

以下是我更新的依赖项

"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"
 }

共有3个答案

谢建业
2023-03-14

基于Klugjos的回答。您可以对React的功能组件(FC)做同样的事情,并使用useState Hook来管理状态。

import React, {FC} from 'react';
import "./Recipe.css";

interface IRecipeProps {
  ingredients?: string[];
  title?: string;
  img?: string;
  instructions?: string;
}

interface IRecipeState {
}

const Recipe:FC<IRecipeProps> = (props) => {

    const { ingredients, title, img, instructions} = props;

    ingredients.map(( ingredient, index) => (
        <li key={index}>
          { ingredient}
        </li>
    ));

    return (
        <div className="recipe-card">
            Your render code here
        </div>
    )
}
赏开宇
2023-03-14

您也可以使用

class Recipe extends React.Component<any, any>{

....
....

// The rest of your normal code
}

伍心水
2023-03-14

您需要使用接口和TypeScript的React通用实现来定义您的道具和状态。组成部分

import React, {Component} from 'react';
import "./Recipe.css";

interface IRecipeProps {
  ingredients?: string[];
  title?: string;
  img?: string;
  instructions?: string;
}

interface IRecipeState {
}

class Recipe extends Component<IRecipeProps, IRecipeState> {
    render() {
        const ingredients = this.props.ingredients.map((ing, ind) => (
            <li key={ind}>{ing}</li>
        ));
        const {title, img, instructions} = this.props

        return (
            <div className="recipe-card">
                Your render code here
            </div>
        )
    }
}
  • 我会将文件扩展名改为。tsx指示它是一个使用TypeScript的React文件-
 类似资料:
  • 我是新来的Spring靴。我在基于javax的验证上遇到了麻烦。控制器期望从POSTendpoint获得json obj的列表。在JSON请求json数组中,如果或为空,我得到正确的400错误。但是,当或为空时,它会抛出500个带有错误消息的异常: 控制器。JAVA 汽车JAVA Engine.java CollectionValidator。JAVA 验证者Advice.java 请求/响应:U

  • 您好,我正在建立一个动物园微服务,包括动物、员工、客户和价格表。我的动物微服务可以工作,但在我的员工微服务中,我遇到了一个错误,即没有为类型“EmployeeModel”找到属性“name”。在问这个问题之前,我已经在网上搜索了几个小时,还有一些类似的问题。我在模型中没有“名字”employee_name,我只是感到困惑,不知道如何修复它。任何指导/建议/信息将不胜感激:)第一次发布,所以我希望我

  • 是否有任何方法可以在模型部分定义HashMap或泛型对象类型?我有一个返回产品的REST服务,这些产品可以有不同的选择。options属性基本上是一个HashMap,其中id是选项名,其值是选项值。

  • 我使用的是完全修补过的Visual Studio 2013。我正在尝试使用JQuery、JQueryUI和JSRender。我也在尝试使用打字。在ts文件中,我得到如下错误: 类型“{}”上不存在属性“fade div”。

  • 我试图在登录过程后获得连接的用户模型, 首先,在CanActivate guard检查了带有用户JSON对象的本地存储并发现该对象为空之后,用户将重定向到“/login”URL 然后用户登录并重定向回根URL“/”,在连接过程中我使用AuthService,它将从服务器返回的用户对象保存在用户模型中 这是我的守卫:

  • 问题内容: 在模型类中使用JavaFX bean属性是否正确? 我想知道在模型类中使用属​​性是否能够将它们与视图组件轻松绑定是否是一种好习惯。我不担心将来这些库的可用性,因为我的程序将在JRE8或更高版本上运行,但是在模型类中使用JavaFX库的性质使我持怀疑态度,并且我担心当前和将来的不兼容性,尤其是因为我想使用Hibernate来保留这些属性。 注意:我使用纯JavaFX环境,并且我的应用程