How do I set the background color on the vue component page?

the current component page is the / test page. I want to set the background color for the whole page, but I find that I can"t. Why?

{
      path: "/test",
      name: "Test",
      component: Test
}
<template>
   <div class="txt">
       <span>aaa</span>
   </div>
</template>

<script>
export default {
  name: "Test",
 }
</script>

<style scoped>
body{width: 100%;height: 100%;position: relative;background-color: -sharpccc}
</style>
Jun.24,2021

change the background color when entering the component page:

  beforeCreate () {
    document.querySelector('body').setAttribute('style', 'background:-sharpfff')
  },

restore the background color when exiting the component page:

beforeDestroy () {
    document.querySelector('body').setAttribute('style', 'background:-sharp***')
  }

notice the scoped

here.
<style scoped>
</style>

the above scoped indicates that this CSS scope is only valid for current domain . Of course, your html node will not take effect without body,.

you can refer to this rookie tutorial style scoped

.

if you definitely need to change the style of body. It is recommended to add styles to the directory where the root node is located , or in the project entry index.js import main.css external reference


globally set the height:100%, root element of body-sharpapp should also set the height:100%, and then set the .txt height to 100%, and then set the background color for txt


component: it is not a complete page containing body, but a component. Vue is a single page, that is, the place and component where you use the component contains only one body.
so you can set the background color to .txt


landlord F12 to see why. You may also need to know what a spa application is.

Menu