Lightning Web Components 组件样式(四)
要将样式与组件进行绑定,需要创建一个同名的样式文件,这样样式将会自动应用到组件
在组件中定义的样式的作用域是属于组件的,这样允许组件可以在不同的上下文中可以复用,
可以阻止其他组件的样式的复写
css 作用域例子
- 重要说明
一个组件的文件夹和文件名是骆驼的情况下,myComponent,myComponent.html,和myComponent.css。
在HTML标记中,camelCase映射到kebab-case。当组件呈现时,<template>标记将替换为标记包含组件的
命名空间 - 一个简单demo
demo 说明,包含了两个组件,
cssParent以及cssChild每个组件包含一个<p>标签,cssParent.css样式定义xx-largep
样式,样式只应用到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 组件样式(四)的更多相关文章
- Lightning Web Components 组件生命周期(六)
组件创建以及渲染流程 组件移除dom 处理流程 组件从dom 移除 组件中的disconnectedCallback() 方法被调用 子组件从dom 移除 每个子组件的disconnectedCall ...
- Lightning Web Components 来自salesforce 的web 组件化解决方案
Lightning Web Components 是一个轻量,快速,企业级别的web 组件化解决方案,官方网站也提供了很全的文档 对于我们学习使用还是很方便的,同时我们也可以方便的学习了解salesf ...
- Lightning Web Components html_templates(三)
Lightning Web Components 强大之处在于模版系统,使用了虚拟dom 进行智能高效的组件渲染. 使用简单语法以声明方式将组件的模板绑定到组件的JavaScript类中的数据 数据绑 ...
- Lightning Web Components 安装试用(一)
Lightning Web Components 简称(lwc) 是一个快速企业级的web 组件化解决方案,同时官方文档很全,我们可以完整的 学习lwc 项目结构 使用npx 官方提供了一个creat ...
- Lightning Web Components 开发指南(二)
Lightning Web Components 是自定义元素使用html 以及现代javascript进行构建. Lightning Web Components UI 框架使用web compon ...
- lightning & web components & templates & slots
lightning & web components & templates & slots Web components, Custom elements, Template ...
- Lightning Web Components 组合(五)
使用组合我们可以用来设计复杂的组件. 组合一些比较小的组件,可以增加组件的重新性以及可维护性. 通过以下一个简单的demo,将会展示关于owner 以及container 的概念,在实际的项目中 ex ...
- 前端组件化-Web Components【转】
以下全部转自:http://www.cnblogs.com/pqjwyn/p/7401918.html 前端组件化的痛点在前端组件化横行的今天,确实极大的提升了开发效率.不过有一个问题不得不被重视,拟 ...
- Web Components之Custom Elements
什么是Web Component? Web Components 包含了多种不同的技术.你可以把Web Components当做是用一系列的Web技术创建的.可重用的用户界面组件的统称.Web Com ...
随机推荐
- [Centos 7]MYSQL 安装及登录问题
1. Centos 7 上安装mysql 8 rpm -qa |grep -i mysql //看自己系统有没有装mysql wget https://dev.mysql.com/get/mysql8 ...
- Java异常体系结构学习笔记
异常类的继承层次 1.Throwable是所有异常类的父类,他也继承自Object.所以Throwable是一个类,而不是接口. 2.Error这个分支的异常是由于Java虚拟机内部错误导 ...
- Cascader 级联选择器hover选择效果
官网例子 <div class="block"> <span class="demonstration">hover 触发子菜单< ...
- bootstrap-combined.min.css 与 bootstrap.css冲突
使用bootstrap-paginator.js分页组件时,根据github上的demo,需要引入下列css: <link href="//netdna.bootstrapcdn.co ...
- Workerman简单开发示例实践(二)
一.在Workerman目录下创建ws_test.php,如下图,并输入如下代码: <?php use Workerman\Worker; require_once __DIR__ . '/Au ...
- 【转】Flex 布局教程:语法篇
作者: 阮一峰 日期: 2015年7月10日 网页布局(layout)是 CSS 的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + floa ...
- CRC-CCITT CRC-16
CRC分为以下几种标准: CRC-12码 CRC-16码 CRC-CCITT码 CRC-32码 在线CRC计算器 https://www.lammertbies.nl/comm/info/crc-ca ...
- AppDir【创建缓存目录】【建议使用这个工具类】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 创建缓存目录 public static String APP_CACHE = "";// /storage/e ...
- SpringBoot上传文件报错,临时路径不存在
异常信息 报错日志: The temporary upload location [/tmp/tomcat.7957874575370093230.8088/work/Tomcat/localhost ...
- 整型 字符串方法 for循环
整型 # 整型 -- 数字 (int) # 用于比较和运算的 # 32位 -2 ** 31 ~ 2 ** 31 -1 # 64位 -2 ** 63 ~ 2 ** 63 -1 # + - * / // ...