天天看點

vue設定單頁背景圖檔,不影響其他頁面

想設定某一頁的背景圖檔,其他頁不受影響。起初,我在style中設定

<style>
body{
  background-image: url("../assets/bg5.jpg");
  background-size: cover;
  background-repeat: no-repeat;
}
</style>
           

但會使其他頁面被影響,導緻所有頁面都有這個背景圖檔。

解決方法:

mounted() {
    document.querySelector('body').setAttribute('style', "background-image: url("+require("../assets/bg5.jpg")+");background-size: cover;" +
      "background-repeat: no-repeat")
  },
  beforeDestroy() {
   document.querySelector('body').removeAttribute('style')
  },
           

這樣,當打開此頁面時,顯示它的背景圖檔,離開頁面後,背景銷毀。