0

代码:

let component = ReasonReact.statelessComponent("Page");
type matchParams = {.
  id: int
};
type match = {.
  params: matchParams
};
type userProps = {.
  match: match
};
let home = <Home />;
let user = (~props:userProps) => <User id=props.match.params.id />;
let make = (_children) => {
  ...component,
  render: (_self) =>
    <div> <Route key="home" exact=false path="/home" component=home />
    <Route key="user" exact=false path="/user/:id" component=user /> </div>
};

输出:

未绑定记录字段匹配

在行

let user = (~props:userProps) => <User id=props.match.params.id />;

如何定义类型,我做错了什么?

4

1 回答 1

1

该错误是由match保留关键字引起的。不过,您应该收到更好的错误消息,我认为这是 Reason 中的错误。要解决它,如果你需要它match在 JS 端编译成,你可以使用它_match来代替,否则就使用不同的名称。

您还(可能)还有其他一些问题:

  1. let home = <Home />不是像user. 它可能需要let home = () => <Home/>

  2. 您正在将记录类型定义为函数props将提供给您的记录类型component。但是您可能会得到一个 JS 对象而不是记录。请参阅解释差异的原因指南。

于 2017-12-05T22:35:57.217 回答