简体   繁体   English

类型“{}”中缺少属性“profileStore”,但类型“只读”中需要<AppProps> &#39;.ts(2741)

[英]Property 'profileStore' is missing in type '{}' but required in type 'Readonly<AppProps>'.ts(2741)

I am using mobx react a type script我正在使用 mobx react 一个类型脚本

Why does <MainNote/> show the error为什么<MainNote/>显示错误

Do i just need to set default props?我只需要设置默认道具吗?

在此处输入图片说明

Property 'profileStore' is missing in type '{}' but required in type 'Readonly<AppProps>'.ts(2741)
MainNote.tsx(9, 3): 'profileStore' is declared here.
(alias) class MainNote
import MainNote

I do not want to pass the prop in as it is a prop ejected by mobx.我不想传递道具,因为它是由 mobx 弹出的道具。

Thanks for the help谢谢您的帮助


The code is below代码如下


 import React, { Component } from 'react'; import MainNote from './MainNote'; import { observable, computed } from 'mobx' import { observer, inject, IStoresToProps } from 'mobx-react' import { IStore } from '../interfaces/store/IStore' import style from '../styles/App.module.css'; import { IProfileStore } from '../interfaces/Profile/IProfileStore'; import { iProfile } from '../interfaces/Profile/iProfile'; interface AppProps { profileStore?: IProfileStore } export interface IProfileStore { profile: iProfile, counter: number, loadProfile?: () => void } @inject("profileStore") @observer class App extends Component<AppProps> { static defaultProps = { profileStore: {counter: 0}}; render() { return ( <div className={`container-fluid w-100 h-100 ${style.background}`}> <MainNote/> {console.log('props', this.props.profileStore) } </div> ); } } export default App;

 import React, { Component } from 'react'; import { observable, computed } from 'mobx' import { observer, inject, IStoresToProps } from 'mobx-react' import style from '../styles/MainNote.module.css' import { IStore } from '../interfaces/store/IStore' import {IProsStore} from '../interfaces/store/IPropsStore' interface AppProps { profileStore: IProfileStore; } interface IProfileStore { profile: iProfile; counter: number; loadProfile?: () => void; } interface iProfile { Details: iDetails; Address: iAddress; Notes: iNote[]; } interface iDetails { Name: string; Email: string; Age: number; CellNumber: number; } interface iAddress { No: number; Road: string; Street: string; Place: string; } interface iNote { Date: Date | string; Subject: string; Text: string; Private: boolean; Archived: boolean; } @inject("profileStore") @observer class MainNote extends Component<AppProps> { render() { const { Address } = this.props.profileStore.profile; console.log('s', this.props.profileStore.profile.Address.No) return ( <div className={style.makeLookCool}> <ul className="list-group"> <li className="list-group-item">{Address.No} {Address.Place} {Address.Road} {Address.Street}</li> </ul> </div> ); } } export default MainNote;

 import React from 'react'; import ReactDOM from 'react-dom'; import 'bootstrap/dist/css/bootstrap.css'; import './styles/index.css'; import App from "./componenets/App"; import { Provider } from 'mobx-react'; import {IProsStore} from './interfaces/store/IPropsStore' import * as serviceWorker from './serviceWorker'; // import NotesStore from "./NotesStore"; // import CounterStore from "./CounterStore"; import ProfileStore from './store/ProfileStore'; import { IStore } from './interfaces/store/IStore'; const Store: IStore = { profileStore: new ProfileStore(), }; ReactDOM.render( <Provider {...Store}> <App /> </Provider>, document.getElementById('root'));

Make profileStore props mandatory from:从以下方面强制使用 profileStore 道具:

interface AppProps {
  profileStore: IProfileStore;
}

to optional可选

interface AppProps {
  profileStore?: IProfileStore;
}

答案是在组件中设置默认的prop

  static defaultProps = {profileStore:{}}

For others who see the same error.对于看到相同错误的其他人。

The same problem happens when you use withStyles and you import that class in a curly braces like the following:当您使用withStyles并在花括号中导入该类时会发生同样的问题,如下所示:

    import { MainNote } from './MainNote';

this imports directly the class itself without applying the styles and therefore it lacks the classes .这直接导入本身而不应用样式,因此它缺少classes You just need to remove the curly braces to import the default export:您只需要删除花括号即可导入默认导出:

    import MainNote from './MainNote';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型“{}”中缺少属性“x”,但类型“Pick”中需要<Interface, "x"> &#39;。 TS2741 - Property 'x' is missing in type '{}' but required in type 'Pick<Interface, "x">'. TS2741 TS2741 类型 &#39;{ 标签:字符串; 中缺少属性 &#39;0&#39; Javascript - TS2741 Property '0' is missing in type '{ label: string; } Javascript 类型中缺少属性,但类型中需要属性 - Property is missing in type but required in type 类型“ {}”不可分配给类型“只读” <IIdeasContainerProps> 类型“ {}”中缺少属性“…” - Type '{}' is not assignable to type 'Readonly<IIdeasContainerProps>' Property '…' is missing in type '{}' 类型“{}”中缺少属性“submitAction”,但在类型中是必需的 - Property 'submitAction' is missing in type '{}' but required in type Typescript 错误:类型中缺少属性“children”,但类型“CommonProps”中是必需的 - Typescript error: Property 'children' is missing in type but required in type 'CommonProps' 类型中缺少 TypeScript 属性“导航”,但类型“道具”React Native 中需要 - TypeScript Property 'navigation' is missing in type but required in type 'Props' React Native 类型“ any []”中缺少属性“ 0”,但是类型“ [{{id:string; gp:布尔值; }] - Property '0' is missing in type 'any[]' but required in type '[{ id: string; gp: boolean; }] “ReactElement”类型中缺少属性“...”<any, any> ' 但在类型 '{...}' 中是必需的</any,> - Property '...' is missing in type 'ReactElement<any, any>' but required in type '{...}' '{ rating: any; 类型中缺少属性 'onClick' }' 但在“RatingType”类型中是必需的 - Property 'onClick' is missing in type '{ rating: any; }' but required in type 'RatingType'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM