我在我的項目中使用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>
);
};
入住這http://stackoverflow.com/questions/35374257/error-window-not-defined-in-node-js似乎是一個類似的問題 – Shota