首页 > 其他分享 >Vue 中组件的局部css样式配置:scoped样式

Vue 中组件的局部css样式配置:scoped样式

时间:2022-12-04 22:22:16浏览次数:50  
标签:School Vue name 样式 App vue scoped



Vue 中组件的局部css样式配置:scoped样式

1:说明:


<!--
 ## scoped样式

1. 作用:让样式在局部生效,防止冲突。
2. 写法:```<style scoped>```
 -->


2:代码结构

image


3:代码内容

index.html

<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="utf-8">
    <!-- 针对ie浏览器的一个特殊配置,含义是让ie浏览器以最高的渲染级别进行渲染界面 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- 开启移动端理想视口 -->
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <!-- 配置页签的图标 -->
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <!-- 配置网页的标题:找 package.json文件里的 "name": "vue_test" 值 -->
    <title><%= htmlWebpackPlugin.options.title %></title>
  </head>
  <body>
    <!-- 如果浏览器不支持js,则该标签的元素里的内容将会被渲染 -->
    <noscript>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <!-- 容器 -->
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>


vue.config.js


const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
  lintOnSave:false /*关闭语法检查*/
})



main.js


 //引入Vue
import Vue from 'vue'
//引入App
import App from './App.vue'
//关闭Vue生产提示
Vue.config.productionTip=false;

// 创建Vm
const vm = new Vue(  {
        el:'#app',
        render: (h) => h(App)
   });


App.vue

 <!--

 -->

<template>
  <div>
    <hr/>
    <div class="divCss">
        <h1 class="h1Css">Vue中的scoped控制组件的Css样式为局部样式</h1>
        <span style="color:white;font-size: 14px;padding: 20px;margin-left: 20px;"> App组件的css样式不允许进行 scoped进行局部样式控制。</span>
        <hr/>
        <!-- 调用组件,传递数据 -->
        <School/>
        <hr/>
        <Student />
        <hr/>

    </div>

  </div>
</template>
<script>

    import School from './components/School.vue';
    import Student from './components/Student.vue';


    export default {
        name:'App',
        components:{
            Student,
            School
        },
    }
</script>
<!--
  1:指定Css 样式的编写方式
  less 最大特点:内容可以嵌套着写

 -->
<style lang="less">
   .divCss{
        background-color: chocolate;
        margin: auto;
        padding: 20px;

        .h1Css{
        font-size: 36px;
        color: white;
   }
   }

</style>

Student.vue

<!--
 ## scoped样式

1. 作用:让样式在局部生效,防止冲突。
2. 写法:```<style scoped>```
 -->
<template>
    <div  class="ClassStyle">
      <h1  style="color:red">{{msg}}</h1>
      <h2>学生名称:{{name}}</h2>
      <h2>学生性别:{{sex}}</h2>
      <br/>
    </div>
</template>
  <script>
      export default {
        name:'Student',
        data () {
          return {
            msg:'学生信息',
            name:'向北',
            sex:'男'
          }
        },

      }
  </script>
 <!--scoped 控制组件的Css样式为局部样式,不会为同名的cs样式名的冲突导致样式覆盖  -->
 <style  scoped>
    .ClassStyle{
      background-color:lightskyblue;
    }
  </style>

School.vue

<!--
  ## scoped样式

  1. 作用:让样式在局部生效,防止冲突。
  2. 写法:```<style scoped>```
 -->
<template>
    <div  class="ClassStyle">
      <h1  style="color:red">{{msg}}</h1>
      <h2>学校名称:{{name }}</h2>
      <h2>学校地址:{{address}}</h2>
    </div>
</template>
  <script>
      export default {
        name:'School',
        data () {
          return {
            msg:'学校信息',
            name:'华南师范大学',
            address:'广州市天河区中山大道西55号'
          }
        },

      }
  </script>
  <!--scoped 控制组件的Css样式为局部样式,不会为同名的cs样式名的冲突导致样式覆盖  -->
  <style  scoped>
    .ClassStyle{
      background-color: aquamarine;
    }
  </style>










4:页面效果图


image









标签:School,Vue,name,样式,App,vue,scoped
From: https://www.cnblogs.com/ios9/p/16951001.html

相关文章

  • vue3+echarts,tooltip trigger: 'axis'没有作用,解决办法
    用markRaw让echarts从监听对象变成普通对象!!因为vue3中的数据对象是用的proxy监听的,要取值需要用value等方法取出来。import{onMounted,ref,markRaw}from'vue'......
  • 转 vue中的双向数据绑定详解 的解释
    vue中的双向数据绑定详解 https://www.cnblogs.com/zhuzhenwei918/p/7309604.html 前言什么是数据双向绑定? vue是一个mvvm框架,即数据双向绑定,即当数据发生......
  • Blazor和Vue对比学习(进阶.路由导航一):基本使用
    Blazor和Vue都是单文件组件SPA,路由的实现逻辑非常相似,页面路径的改变都是组件的切换,但因为各自语言的特性,在实现方式上有较大差异。 一、安装1、Vue:Router是Vue的一个......
  • VUE3
    ######computed```javascriptconstcomputed1=computed({get(){returncount},set(){console.log("1")}})``` ######watch```javascriptconstco......
  • 常用Vue UI框架的主题切换
    在如今,很多网页已经可以手动切换明亮模式和黑暗模式,网页的主题切换已经成为了一个常用的需求,因此,本文将从常见框架的处理方式总结一些相关的操作,并探究其本质。AntD的方......
  • uniapp和vue的区别
    https://m.php.cn/article/481418.html uniapp和vue的区别uni-app可以编译到(头条,支付宝,微信,QQ,百度)小程序,安卓版,ios版,h5版。通过打包实现一套代码多端运行;vue在web上是......
  • js:Vue2.js通过CDN方式引入模板
    文档https://v2.cn.vuejs.org/v2/guide/installation.html示例index.html<!--引入Vue2.7.14--><scriptsrc="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.......
  • vue项目创建步骤
    (1).安装可以反复生成脚手架的工具:    a.设置淘宝镜像——下载快 npmconfigsetregistryhttps://registry.npm.taobao.org b.安装可生成脚手架代码的命令行工......
  • Vue中优雅的更改iframe嵌入页面的样式
    通过外部引入css文件来控制嵌入页面的样式公共iframe组件封装传入属性:嵌入页面路径css文件名称(默认放在/static/css/下),默认css文件名可以自己定义,在确定嵌入页面不多,相......
  • 【vue-router 4.x】使用addRoute加载动态路由时,刷新页面后出现空白页和控制台报错 [Vu
    "vue-router":"^4.1.6"遇到的问题动态路由刷新后,出现空白页动态路由刷新后,控制报错[VueRouterwarn]:Nomatchfoundforlocationwithpath"/***/index"1.动态......