vue-cli样式问题

最新更新:
banner.vue

<template>
  <div class="banner">
    <img :src="banner" alt="banner图">
    <div class="banner-text">
      <h3 class="title-text" v-if="title_text">{{title_text}}</h3>
      <p class="content-text" v-html="content_text"></p>
    </div>
  </div>
</template>
<script>
  export default {
    name: 'Banner',
    data() {
      return {

      }
    },
    props: {
      banner: {
        type: String,
        required: true
      },
      title_text: String,
      content_text: String
    }
  }

</script>
<style lang="css">
  /* banner图 */
  .banner {
    position: relative;
    font-size: 0;
  }

  .banner img {
    width: 100%;
  }

  /* banner+文字 */
  .banner .banner-text {
    position: absolute;
    width: 800px;
    height: 100%;
    top: 0;
    left: 50%;
    margin-left: -400px;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .banner .banner-text .title-text {
    font-size: 30px;
    color: #ffffff;
    text-align: center;
    /* margin-top: 180px; */
  }

  .banner .banner-text .content-text {
    font-size: 16px;
    color: #ffffff;
    /* text-align: center; */
    margin-top: 90px;
    line-height: 28px;
  }

</style>

index.vue

<template>
  <div id="wrap">
    <header-menu></header-menu>
    <banner-text :banner="banner_src" :title_text="banner_title" :content_text="banner_text" class="top-banner">
    </banner-text>
    <banner-text :banner="index_img" :title_text="title_text" :content_text="content_text"></banner-text>
    <footer-menu></footer-menu>
  </div>
</template>
<script>
  import headerMenu from '../components/Header'
  import bannerText from '../components/Banner'
  import footerMenu from '../components/Footer'
  export default {
    data() {
      return {
        banner_src: '../../static/img/banner.png',
        banner_title: '第一张banner标题',
        banner_text: '第一张banner文字',
        index_img: '../../static/img/banner1.png',
        title_text: '第二张banner标题',
        content_text: '第二张banner文字',
      }
    },
    components: {
      headerMenu,
      bannerText,
      footerMenu,
    }
  }

</script>
<style lang="css" scoped>
  .banner.top-banner .banner-text {
    width: auto;
    height: auto;
    right: 360px;
    top: 145px;
    left: auto;
  }

  .banner.top-banner .banner-text .title-text {
    font-size: 54px;
  }

  .banner.top-banner .banner-text .content-text {
    font-size: 44px;
    margin-top: 44px;
    line-height: 44px;
    letter-spacing: 3px;
  }

  @media screen and (max-width:1200px) and (min-width:800px) {
    .top-banner .banner-text {
      width: auto;
      height: auto;
      right: 100px;
      top: 80px;
      left: auto;
    }

    .top-banner .banner-text .title-text {
      font-size: 36px;
    }

    .top-banner .banner-text .content-text {
      font-size: 28px;
      line-height: 28px;
      margin-top: 28px;
    }

    #introduce .box {
      width: 190px;
      height: 220px;
    }
  }

</style>

about.vue

<template>
  <div id="wrap">
    <header-menu></header-menu>
    <banner-text :banner="about_banner_src" :content_text="content_text"></banner-text>
    <footer-menu></footer-menu>
  </div>
</template>
<script>
  import headerMenu from '../components/Header'
  import bannerText from '../components/Banner'
  import footerMenu from '../components/Footer'
  export default {
    data() {
      return {
        about_banner_src: '../../static/img/banner4.png',
        content_text: 'about banner文字'
      }
    },
    components: {
      headerMenu,
      bannerText,
      footerMenu
    }
  }

</script>
<style lang="css" scoped>
  #wrap .banner .banner-text {
    width: 1024px;
    margin-left: -512px;
  }

  #wrap .banner .banner-text .content-text {
    margin-top: -35px;
  }

  @media screen and (max-width:1200px) and (min-width:800px) {
    #wrap .banner .banner-text {
      width: 750px;
      margin-left: -375px;
    }

    #wrap .banner .banner-text .content-text {
      margin-top: -20px;
    }
  }

</style>

对about里的css加scoped之后,index不出现about的样式了,但是about的样式又覆盖不了banner的样式。。。


image.png
如题,为什么index.vue页面会出现about页面的样式。
banner组件分别用于index.vue 及 about.vue,
为什么在index.vue会出现about.vue的样式。

阅读 1.9k
2 个回答

vue开发中,父组件添加scoped之后。解决在父组件中无法修改子组件样式问题。

通过以上链接解决了我困扰多时的问题,banner.vue样式全局,index,about样式局部且要覆盖banner的样式,解决方案如上链接,使用/deep/
banner.vue

<template>
  <div class="banner">
    <img :src="banner" alt="banner图">
    <div class="banner-text">
      <h3 class="title-text" v-if="title_text">{{title_text}}</h3>
      <p class="content-text" v-html="content_text"></p>
    </div>
  </div>
</template>
<script>
  export default {
    name: 'Banner',
    data() {
      return {

      }
    },
    props: {
      banner: {
        type: String,
        required: true
      },
      title_text: String,
      content_text: String
    }
  }

</script>
<style lang="css">
  /* banner图 */
  .banner {
    position: relative;
    font-size: 0;
  }

  .banner img {
    width: 100%;
  }

  /* banner+文字 */
  .banner .banner-text {
    position: absolute;
    width: 800px;
    height: 100%;
    top: 0;
    left: 50%;
    margin-left: -400px;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .banner .banner-text .title-text {
    font-size: 30px;
    color: #ffffff;
    text-align: center;
    /* margin-top: 180px; */
  }

  .banner .banner-text .content-text {
    font-size: 16px;
    color: #ffffff;
    /* text-align: center; */
    margin-top: 90px;
    line-height: 28px;
  }

</style>

index.vue

<template>
  <div id="wrap">
    <header-menu></header-menu>
    <banner-text :banner="banner_src" :title_text="banner_title" :content_text="banner_text" class="top-banner">
    </banner-text>
    <banner-text :banner="index_img" :title_text="title_text" :content_text="content_text"></banner-text>
    <footer-menu></footer-menu>
  </div>
</template>
<script>
  import headerMenu from '../components/Header'
  import bannerText from '../components/Banner'
  import footerMenu from '../components/Footer'
  export default {
    data() {
      return {
        banner_src: '../../static/img/banner.png',
        banner_title: '第一张banner标题',
        banner_text: '第一张banner文字',
        index_img: '../../static/img/banner1.png',
        title_text: '第二张banner标题',
        content_text: '第二张banner文字',
      }
    },
    components: {
      headerMenu,
      bannerText,
      footerMenu,
    }
  }

</script>
<style lang="css" scoped>
  .banner.top-banner /deep/ .banner-text {
    width: auto;
    height: auto;
    right: 360px;
    top: 145px;
    left: auto;
  }

  .banner.top-banner /deep/ .banner-text .title-text {
    font-size: 54px;
  }

  .banner.top-banner /deep/ .banner-text .content-text {
    font-size: 44px;
    margin-top: 44px;
    line-height: 44px;
    letter-spacing: 3px;
  }

  @media screen and (max-width:1200px) and (min-width:800px) {
    .top-banner /deep/ .banner-text {
      width: auto;
      height: auto;
      right: 100px;
      top: 80px;
      left: auto;
    }

    .top-banner /deep/ .banner-text .title-text {
      font-size: 36px;
    }

    .top-banner /deep/ .banner-text .content-text {
      font-size: 28px;
      line-height: 28px;
      margin-top: 28px;
    }

    #introduce .box {
      width: 190px;
      height: 220px;
    }
  }

</style>

about.vue

<template>
  <div id="wrap">
    <header-menu></header-menu>
    <banner-text :banner="about_banner_src" :content_text="content_text"></banner-text>
    <footer-menu></footer-menu>
  </div>
</template>
<script>
  import headerMenu from '../components/Header'
  import bannerText from '../components/Banner'
  import footerMenu from '../components/Footer'
  export default {
    data() {
      return {
        about_banner_src: '../../static/img/banner4.png',
        content_text: 'about banner文字'
      }
    },
    components: {
      headerMenu,
      bannerText,
      footerMenu
    }
  }

</script>
<style lang="css" scoped>
 .banner /deep/ .banner-text {
    width: 1024px;
    margin-left: -512px;
  }

   .banner /deep/ .banner-text .content-text {
    margin-top: -35px;
  }

  @media screen and (max-width:1200px) and (min-width:800px) {
   .banner /deep/ .banner-text {
      width: 750px;
      margin-left: -375px;
    }

    .banner /deep/ .banner-text .content-text {
      margin-top: -20px;
    }
  }

</style>

在组件中写的style在不加scope的情况下是全局作用的

<style lang="scss" scope>
.body {
    color: red;
}
<style>

加上scope就不会污染全局样式了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题