2016-11-24 62 views
0

我目前有一個<Login/>頁面和一個<Dashboard/>刪除componentIdMount上的backgroundImage

登錄頁面有#222的背景下,當您登錄儀表板具有whitesmoke

的方式我這樣做是有這個對身體的CSS背景:

body { 
    background-color: #222222; 
} 

和這在Dashboard.js

componentWillMount() { 
    document.body.style.backgroundColor = "whitesmoke"; 
} 
componentWillUnmount() { 
    document.body.style.backgroundColor = null; 
} 

到目前爲止,這是工作。但是,我現在有一個圖片作爲我的登錄頁面上的背景,如下所示:

body { 
    background-color: #222222; 
    background: url('../../public/img/bg.png'); 
    background-repeat: repeat; 
} 

,但我的儀表盤繼承了背景圖像,甚至當我把這樣的事情:

componentWillMount() { 
    document.body.style.backgroundImage = null; 
    document.body.style.backgroundColor = "whitesmoke"; 
} 
componentWillUnmount() { 
    document.body.style.backgroundColor = null; 
} 

怎麼辦我解決了這個問題? 謝謝

+1

'null'不是backgroundImage的有效值,它會被瀏覽器忽略。使用'document.body.style.backgroundImage ='none';' – pawel

回答

1

爲什麼不使用類呢?


componentWillMount() { 
    $('body').addClass('has-background'); 
} 
componentWillUnmount() { 
    $('body').removeClass('has-background'); 
} 

此外,您可能要抽象的addClass/removeClass和使用emits