gpt4 book ai didi

javascript - 窗口未定义

转载 作者:行者123 更新时间:2023-11-28 04:41:05 24 4
gpt4 key购买 nike

我在我的项目中使用 ReactJS,我想通过使用 window.location.href 来访问我当前的 URL。当我使用 window.location.href 时,出现错误,提示窗口未定义。

// --- REACT + CUSTOM-TAGS ---
const SampleApp = ({value}) => {
// --- CUSTOM-SCRIPTS ---
addMeta([
{type: 'meta', content: {content: 'something'}},
{type: 'link', content: {rel: 'http://link'}},
]);

//Append the value from the URL
const prId = "12512" //We can make it dynamic
const url = window.location.href;//window,location.href;
const id = url.substring(url.lastIndexOf('/') + 1);
const output = id.replace(/[a-zA-Z=]/g, '');

return (
<div>
<h1>Hello {value}</h1>
<hr />
<h2>{url} took from prId</h2>
<hr />
<h2><a className="redirect" href={'//www.check.com/prId/' + output} target="_blank">Click Here</a>
</h2>
</div>
);
};

最佳答案

位置在 props 中,因为 React 中没有定义 window。

只需执行 this.props.location.pathname 而不是 window.location.href

如果您记录 Prop ,则会获得更多信息。

关于javascript - 窗口未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43763367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com