[英]Type 'string' is not assignable to type '(url: string) => string'.ts(2322)
I have this material ui copyright component:我有这个材料 ui 版权组件:
export default function Copyright(link: string, text: string) {
return (
<Typography variant="body2" color="textSecondary" align="center">
{'Copyright © '}
<Link color="inherit" href={link}>
{text}
</Link>{' '}
{new Date().getFullYear()}
{'.'}
</Typography>
);
}
If I try to use it like this, I don't get any errors:如果我尝试像这样使用它,则不会出现任何错误:
{Copyright('https://hello.com', 'HELLO')}
However, if I try to use it like this:但是,如果我尝试像这样使用它:
<Copyright link={'https://hello.com'} text={'hello'}></Copyright>
I get this error on link even though I have not specified any 'url' anywhere else:即使我没有在其他任何地方指定任何“url”,我仍然在链接上收到此错误:
Type 'string' is not assignable to type '(url: string) => string'.ts(2322)
How can I use this component with the second method?如何将这个组件与第二种方法一起使用? A similar question suggested to use casting but in my case, there can be multiple links with which I want to call the component in the future.
一个类似的问题建议使用强制转换,但在我的情况下,可能有多个链接,我想在将来调用该组件。
If you want to use如果你想使用
<Copyright link={"https://hello.com"} text={"hello"} />
You need to define the props type like this您需要像这样定义道具类型
props: { link: string; text: string }
Usage:用法:
function Copyright(props: { link: string; text: string }) {
return (
<Typography variant="body2" color="textSecondary" align="center">
{"Copyright © "}
<Link color="inherit" href={props.link}>
{props.text}
</Link>{" "}
{new Date().getFullYear()}
{"."}
</Typography>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.