要将样式与组件进行绑定,需要创建一个同名的样式文件,这样样式将会自动应用到组件
在组件中定义的样式的作用域是属于组件的,这样允许组件可以在不同的上下文中可以复用,
可以阻止其他组件的样式的复写

css 作用域例子

  • 重要说明
    一个组件的文件夹和文件名是骆驼的情况下,myComponent,myComponent.html,和myComponent.css。
    在HTML标记中,camelCase映射到kebab-case。当组件呈现时,<template>标记将替换为标记包含组件的
    命名空间
  • 一个简单demo

    demo 说明,包含了两个组件,cssParent 以及cssChild 每个组件包含一个<p>标签,cssParent.css 样式定义xx-large p
    样式,样式只应用到parent p 标签,而不是在child 中的

    标签

cssParent.js

 
import { LightningElement } from 'lwc';
export default class CssParent extends LightningElement {}

cssParent.html

<template>
  <p>To Do List</p>
  <example-css-child></example-css-child>
</template>
 
 

cssParent.css

p {
    font-size: xx-large;
}

cssChild.js

import { LightningElement } from 'lwc';
export default class CssChild extends LightningElement {}

cssChild.html

<template>
     <p>To Do Item</p>
</template>
 
 

cssChild.css

/*
:host {
    display: block;
    background: whitesmoke;
}
*/
 
 

父组件应用样式到child 组件,这个可以通过元素样式的方式

/* cssParent.css */
p {
    font-size: xx-large;
}
example-css-child {
    display: block;
    background: whitesmoke;
}
 
 

子组件样式的应用,可以通过:host 配置样式,以及类似parent 的样式配置

/* cssChild.css */
:host {
    display: block;
    background: whitesmoke;
}
 

同时对于组件我们可以添加<slot></slot> 方便的进行内容填充

css 的支持以及对于性能的影响

  • 一些不支持的css 选择器
    :host-context()
    ::slotted
    不支持id 选择器
  • 对于性能的影响
    每个选择器都有自己作用域链,所以传递给的每个复合表达式:host()都会扩展到多个选择器中。这种转换增加了生成的CSS的大小和复杂性。
    为了确保CSS封装,每个元素都有一个额外的属性,这也增加了渲染时间

参考资料

https://lwc.dev/guide/reference#component-bundles
https://lwc.dev/guide/composition#pass-markup-into-slots

Lightning Web Components 组件样式(四)的更多相关文章

  1. Lightning Web Components 组件生命周期(六)

    组件创建以及渲染流程 组件移除dom 处理流程 组件从dom 移除 组件中的disconnectedCallback() 方法被调用 子组件从dom 移除 每个子组件的disconnectedCall ...

  2. Lightning Web Components 来自salesforce 的web 组件化解决方案

    Lightning Web Components 是一个轻量,快速,企业级别的web 组件化解决方案,官方网站也提供了很全的文档 对于我们学习使用还是很方便的,同时我们也可以方便的学习了解salesf ...

  3. Lightning Web Components html_templates(三)

    Lightning Web Components 强大之处在于模版系统,使用了虚拟dom 进行智能高效的组件渲染. 使用简单语法以声明方式将组件的模板绑定到组件的JavaScript类中的数据 数据绑 ...

  4. Lightning Web Components 安装试用(一)

    Lightning Web Components 简称(lwc) 是一个快速企业级的web 组件化解决方案,同时官方文档很全,我们可以完整的 学习lwc 项目结构 使用npx 官方提供了一个creat ...

  5. Lightning Web Components 开发指南(二)

    Lightning Web Components 是自定义元素使用html 以及现代javascript进行构建. Lightning Web Components UI 框架使用web compon ...

  6. lightning & web components & templates & slots

    lightning & web components & templates & slots Web components, Custom elements, Template ...

  7. Lightning Web Components 组合(五)

    使用组合我们可以用来设计复杂的组件. 组合一些比较小的组件,可以增加组件的重新性以及可维护性. 通过以下一个简单的demo,将会展示关于owner 以及container 的概念,在实际的项目中 ex ...

  8. 前端组件化-Web Components【转】

    以下全部转自:http://www.cnblogs.com/pqjwyn/p/7401918.html 前端组件化的痛点在前端组件化横行的今天,确实极大的提升了开发效率.不过有一个问题不得不被重视,拟 ...

  9. Web Components之Custom Elements

    什么是Web Component? Web Components 包含了多种不同的技术.你可以把Web Components当做是用一系列的Web技术创建的.可重用的用户界面组件的统称.Web Com ...

随机推荐

  1. UI单据按钮点击事件校验

    一.按钮点击前事务处理<BeforeEventProcess> public override void BeforeEventProcess(IPart part, string eve ...

  2. golang基础学习---log

    package main import ( "log" ) func init() { log.SetPrefix("TRACE: ") log.SetFlag ...

  3. 如何配置这个maven仓库的源http://mvnrepository.com/repos

    http://mvnrepository.com/repos 主要是ID .mirrorof.name 怎么配置,这个网站上有spring5.0的,别的仓库没有,我需要这个源. 原文地址:https: ...

  4. SSM 前后端分离 这里controll层的返回值和之前那个不一样

    1.先创建实体类: 2.创建mapper层 package cn.kgc.mapper; import cn.kgc.Account;import org.apache.ibatis.annotati ...

  5. Mysql中use filesort的误区

    误区一字面误区 use filesort排序,字面上理解是外部排序. 误区二人云亦云 百度上多被大家否定不是外部排序,认为和file这个关键字没关系.用的是快速排序.但是总觉得不可能这么无缘无故叫fi ...

  6. JUC - ThreadPoolExecutor

    JUC - ThreadPoolExecutor 创建一个ThreadPoolExecutor ThreadPoolExecutor( int corePoolSize, // 保留在池中的线程数,即 ...

  7. 简述JavaScript模块化编程(一)

    在早期编写JavaScript时,我们只需在<script>标签内写入JavaScript的代码就可以满足我们对页面交互的需要了.但随着时间的推移,时代的发展,原本的那种简单粗暴的编写方式 ...

  8. php学习笔记——学习路线图记录

    PHP学习路线图 最全PHP自学指南 W3Cschool小编 2018-04-24 15:23:51 浏览数 (5381) 分享 收录专辑 对于广大零基础的PHP自学者,往往不知道如何系统的学习PHP ...

  9. Windows下MongoDB的下载安装、环境配置

    下载MongoDB 1.进入MongoDB官网,Products -> 选择SOFTWARE下的MongoDB Server 2.选择下载最新版 3.选择对应的版本下载 msi安装包形式安装Mo ...

  10. 【DATAGUARD】物理dg的switchover切换(五)

    [DATAGUARD]物理dg的switchover切换(五) 一.1  BLOG文档结构图 一.2  前言部分 一.2.1  导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其 ...