跳转页面传参报错
1、Property ‘param1’ does not exist on type ‘Location’. TS2339
原因:react识别不了Location
跳转代码
let path = {
param1: data,
pathname: Route_A001P005,
};
this.history.push(path);
获取参数
useEffect(() => {
console.log("param1:",props.location.param1)
});
单纯输出props有location和param1参数名称和值,但是编译就是报错。
解决办法
先把props.location赋值给一个类型为 any 的常量,然后再用 该常量获取参数
const a = props.location as any;
console.log("param1 :",a.param1)
2、Property ‘location’ does not exist on type ‘PropsWithChildren’. TS2339
报PropsWithChildren错误的原因是获取参数页面的Props 没有继承RouteComponentProps
解决办法
interface Props extends RouteComponentProps<any> {
}