我使用MERN - https://github.com/Hashnode/mern-starter(反應,終極版,的WebPack,的NodeJS表達) 和組分反應聲 - https://github.com/leoasis/react-sound的WebPack渲染: 「沒有定義窗口」
當我包括組分
import Sound from 'react-sound';
並嘗試啓動服務器,我從webpack服務器呈現錯誤"window is not defined"
。
但是,如果我評論這一行並啓動服務器,一切都會好起來的。之後,我可以取消註釋這一行,組件將工作,因爲當服務器運行時,更改不會觸發服務器呈現(只有正面呈現)。
如果我嘗試
if (typeof window !== 'undefined') {
const Sound = require('react-sound');
}
而在渲染
render() {
return (
<div>
<Sound playStatus={Sound.status.STOPPED} url="" />
</div>
);
}
我對前面的渲染(之後的WebPack)ReferenceError: Sound is not defined
錯誤。
UPDATE:
如果我嘗試(var
,不const
)
if (typeof window !== 'undefined') {
var Sound = require('react-sound');
}
我對前面的渲染TypeError: Cannot read property 'status' of undefined
錯誤。
限定內部的常量聲音如果塊限制了範圍,以該塊的內部。將var Sound = null;在if之外,然後在if中設置值。 – scrappedcola