完成:vue-styled-components
总结
import styled,{ThemeProvider,injectGlobal} from 'vue-styled-components';
// 返回组件选项对象
styled.div`...`
// 可通过props传参的组件
const StyledButton = styled('button', { primary: {
type:Boolean,
default:"some"
})`...`
// 通过组件创建样式组件
const RouterLink = Vue.component('router-link')
const StyledLink = styled(RouterLink)`...`
// 主题组件
const Wrapper = styled.section`
background: ${props => props.theme.primary};
`;
<theme-provider :theme="{
primary: 'palevioletred'
}">
<wrapper>
...
</wrapper>
</theme-provider>
// 扩展原有样式组件
const TomatoButton = StyledButton.extend`...`
// 复制样式创建新的样式组件
const Link = Button.withComponent('a')
// 添加全局样式
injectGlobal`...`
——————————————————————————————————————————————————————————————————————————————————
基础
https://es6.ruanyifeng.com/#docs/string#模板字符串
- 注释:模板字符串返回字符串类型
- 如果
${...}中的值不是字符串,将按照一般的规则转为字符串。比如,大括号中是一个对象,将默认调用对象的toString方法 - 模板字符串能够嵌套
- 注释:在
${...}使用另一个模板字符串
- 注释:在
https://es6.ruanyifeng.com/#docs/string#标签模板
- 模板字符串可以紧跟在一个函数名后面,该函数将被调用来处理这个模板字符串。这被称为“标签模板”功能
alert`hello`
// 等同于
alert(['hello'])
- 如果模板字符里面有变量,就不是简单的调用了,而是会将模板字符串先处理成多个参数,再调用函数
- 注释:参数一这个字符串数组是以${}为参考位进行split的,例如
'bacada'.split('a') // ["b", "c", "d", ""]和'abacad'.split('a') // ["", "b", "c", "d"],即当变量在前后头部时会在前后多出一个空字符串的数组项。也可以理解为常量数组比变量参数长度多1 - 注释:参数一字符串数组其他还包含一个名为raw的属性
stringArr.raw,保存的是转义后的原字符串数组。为了方便取得转义之前的原始模板而设计的
- 注释:参数一这个字符串数组是以${}为参考位进行split的,例如
let a = 5;
let b = 10;
tag`Hello ${ a + b } world ${ a * b }`;
// 等同于
tag(['Hello ', ' world ', ''], 15, 50);
function tag(stringArr, value1, value2){
// ...
}
// 等同于
function tag(stringArr, ...values){
// ...
}
- “标签模板”的一个重要应用,就是过滤 HTML 字符串,防止用户输入恶意内容
function SaferHTML(templateData) {
let s = templateData[0];
for (let i = 1; i < arguments.length; i++) {
let arg = String(arguments[i]);
// Escape special characters in the substitution.
s += arg.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
// Don't escape special characters in the template.
s += templateData[i];
}
return s;
}
let sender = '<script>alert("abc")</script>'; // 恶意代码
let message = SaferHTML`<p>${sender} has sent you a message.</p>`;
// <p><script>alert("abc")</script> has sent you a message.</p>
- 标签模板的另一个应用,就是多语言转换(国际化处理)
i18n`Welcome to ${siteName}, you are visitor number ${visitorNumber}!`
// "欢迎访问xxx,您是第xxxx位访问者!"
- 使用标签模板,在 JavaScript 语言之中嵌入其他语言
- 疑问:vue中的jsx是否采用同样的方式实现?可以帮助理解JSX的属性绑定
- 疑问:在JS中执行其他语言的代码,实际上是文本处理后转化为相同功能的当前语言后执行?
jsx`
<div>
<input
ref='input'
onChange='${this.handleChange}'
defaultValue='${this.state.value}' />
${this.state.value}
</div>
`
- 模板处理函数的第一个参数(模板字符串数组),还有一个raw属性,保存的是转义后的原字符串数组。为了方便取得转义之前的原始模板而设计的
- 疑问:JS中各种遍历的本质和区别?
- 注释:
\n解析为文本换行,在字符串和模板字符串中都一样。要定义'\n'字符串应转义为\\n
tag`First line\nSecond line`
function tag(strings) {
console.log(strings.raw[0]);
// strings.raw[0] 为 "First line\\nSecond line"
// 打印输出 "First line\nSecond line"
}
https://es6.ruanyifeng.com/#docs/string#模板字符串的限制
- 模板字符串默认会将
\u当作Unicode 字符、\x当作十六进制字符和\n进行转义,前两个由于格式问题可能导致转义失败,这时包含该字符串的数组项将被填充为undefined,在raw中能拿到转义前字符串- 疑问:在其他语言中如果存在${}的语法应该也会导致解析有误
function tag(strs) {
strs[0] === undefined
strs.raw[0] === "\\unicode and \\u{55}";
}
tag`\unicode and \u{55}`
————————————————————————————————————————————————————————————————————————————
https://github.com/styled-components/vue-styled-components
- 使用建立在language-sass和language-css之上的CSS语法
Support
yarn add vue-styled-components
Usage
Basic
- 注释:styled生成的组件默认含有插槽
- 注释:styled.div返回的是组件的选项对象
import styled from 'vue-styled-components';
const App = styled.div`
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
`;
const Nav = styled.div`
padding: 30px;
a {
font-weight: bold;
color: #2c3e50;
&.router-link-exact-active {
color: #42b983;
}
}
`
export default {
render() {
return <App>
<Nav>
<router-link to="/">Home</router-link> | <router-link to="/about">About</router-link>
</Nav>
<router-view />
</App>
}
}
Adapting based on props
- 注释:可以传入参数控制的styled组件,带有props
- 注释:这是一个真实的vue props选项,遵循vue的方式
import styled from 'vue-styled-components';
const btnProps = { primary: Boolean };
const StyledButton = styled('button', btnProps)`
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
background: ${props => props.primary ? 'palevioletred' : 'white'};
color: ${props => props.primary ? 'white' : 'palevioletred'};
`;
export default StyledButton;
<styled-button primary>Primary</styled-button>
Theming
- 注释:ThemeProvider只需要在根结点绑定theme对象,采用inject方式向子孙组件传递theme对象,所以普通的对象是不具备响应的
- 注释:ThemeProvider是一个组件选项对象
- 疑问:styled.default.div在实际情况中报错,是笔误?
- 注释:Wrapper中的props.theme并非一个真实的props选项
import styled,{ThemeProvider} from 'vue-styled-components'
const Wrapper = styled.default.section`
padding: 4em;
background: ${props => props.theme.primary};
`;
<theme-provider :theme="{
primary: 'palevioletred'
}">
<wrapper>
// ...
</wrapper>
</theme-provider>
Style component constructors as router-link
- 注释:styled作为函数时第一个参数不仅可以传入原生标签名字符串还可以传入组件的构造函数来创建样式组件
- 疑问:传入构造函数应该可以和传入字符串一样使用第二参数创建props供外部传入样式变量
- 疑问:
Vue.component('router-link')不能正确获取组件函数,正确应为Vue.component('RouterLink')
import styled from 'vue-styled-components';
const RouterLink = Vue.component('router-link')
const StyledLink = styled(RouterLink)`
color: palevioletred;
font-size: 1em;
text-decoration: none;
`;
<styled-link to="/">Custom Router Link</styled-link>
- 使用
.extend方法扩展已经定义好了的样式组件,保留组件原有样式并加以扩展或覆盖
import StyledButton from './StyledButton';
const TomatoButton = StyledButton.extend`
color: tomato;
border-color: tomato;
`;
export default TomatoButton;
withComponent
- 注释:可以复制其他组件的样式生成一个新的组件
const Button = styled.button`
background: green;
color: white;
`
const Link = Button.withComponent('a')
injectGlobal
- 全局样式,每个应用最多使用一次
- 注释:可以在多个组件内使用,使用一次并不是硬性规定
// global-styles.js
import { injectGlobal } from 'vue-styled-components';
injectGlobal`
@font-face {
font-family: 'Operator Mono';
src: url('../fonts/Operator-Mono.ttf');
}
body {
margin: 0;
}
`;
Syntax highlighting
- 在vscode中vscode-styled-components插件用来支持语法高亮
- 注释:目前该插件不支持
styled('div',propsObj)...这样的高亮提醒 .extend...这个方法也不支持高亮
————————————————————————————————————————————————————————————————————————————
https://github.com/Microsoft/typescript-styled-plugin
- 注释:用于在ts中实现styled-components高亮提醒,及相关设置
- 疑问:vscode-styled-components插件在JS中存在问题,是否在TS中表现会更好点?
Usage
With VS Code
- 疑问:如果使用的是工作区版本的TypeScript,则必须在工作区中的TypeScript版本旁边手动安装插件?
Configuration
Tags
- 可以通过配置为其他标记名称启用IntelliSense
- 注释:当需要使用sty来代替styled时需要进行配置
- 注释:在JS中无法生效,不知道是不是配置文件错误
// tsconfig.json
{
"compilerOptions": {
"plugins": [
{
"name": "typescript-styled-plugin",
"tags": [
"styled",
"css",
"sty"
]
}
]
}
}
import sty from 'styled-components'
sty.button`
color: blue;
`
Linting
- 疑问:禁用错误报告,TS中对模板语法会出现错误吗?
{
"compilerOptions": {
"plugins": [
{
"name": "typescript-styled-plugin",
"validate": false
}
]
}
}
- 还可以配置使用linter设置报告错误的方式
{
"compilerOptions": {
"plugins": [
{
"name": "typescript-styled-plugin",
"lint": {
"vendorPrefix": "error",
"zeroUnits": "ignore"
}
}
]
}
}
- 支持以下选项
- 注释:略,需要eslint基础
Emmet in completion list
- 已经默认包括Emmet的输入提醒
- 疑问:在TS中似乎有问题,需要通过Ctrl+Space显示输入提醒
- 支持以下选项
- 注释:略,需要Emmet基础
完成:vue-styled-components的更多相关文章
- styled components草根中文版文档
1.styled components官网网址 https://www.styled-components.com/docs 以组件的形式来写样式. 1.1安装 yarn add styled-c ...
- vue ui components
vue ui components h_ui https://www.npmjs.com/~hs_ui https://www.npmjs.com/package/h_ui_beta https:// ...
- [Vue @Component] Handle Errors and Loading with Vue Async Components
Because async components are not bundled with your app, they need to be loaded when requested. This ...
- vue & npm & components & plugins
vue & npm & components & plugins how to publish an vue ui component to npm? https://www. ...
- vue & dynamic components
vue & dynamic components https://vuejs.org/v2/guide/components-dynamic-async.html keep-alive htt ...
- [Vue @Component] Dynamic Vue.js Components with the component element
You can dynamically switch between components in a template by using the reserved <component> ...
- [Vue @Component] Load Vue Async Components
Vue provides a straight-forward syntax for loading components at runtime to help shave off initial b ...
- [Vue @Component] Write Vue Functional Components Inline
Vue's functional components are small and flexible enough to be declared inside of .vue file next to ...
- [Vue] Load components when needed with Vue async components
In large applications, dividing the application into smaller chunks is often times necessary. In thi ...
- [Vue] Dynamic Vue.js Components with the component element
You can dynamically switch between components in a template by using the reserved <component> ...
随机推荐
- 如何快速搭建一个 Node.JS 项目并进入开发?
了解:如何快速搭建一个项目并进入开发? 在此不概述 Node.JS 的历史以及发展过程. 因为之前接触过通过 Java 开发语言,所以明确地知道一个服务器所需的文件,以及一个服务器所需要的操作. 那么 ...
- Linux/UNIX编程:获取指定用户所有正在运行的进程ID和进程名
先用系统函数 `getpwnam` 获得指定用户名的 UID,然后遍历 /proc/ 中所有 PID 目录,如果 /proc/PID/status 中的 UID 是输入用户名对应的 UID 则输出该 ...
- MySQL8.0数据库基础教程(二)-理解"关系"
1 SQL 的哲学 形如 Linux 哲学一切都是文件,在 SQL 领域也有这样一条至理名言 一切都是关系 2 关系数据库 所谓关系数据库(Relational database)是创建在关系模型基础 ...
- php扩展模块的安装
PHP的扩展模块安装 模块安装总则: 进入到ext/目录下对应的模块 执行/usr/local/php/bin/phpize 也就是执行一遍phpize生成编译文件 ./configure --wit ...
- 罗德里格斯旋转公式(Rodrigues' rotation formula)推导
本文综合了几个相关的维基百科,加了点自己的理解,从比较基础的向量投影和叉积讲起,推导出罗德里格斯旋转公式.公式比较繁杂,如有错误,欢迎评论区指出. 对于向量的三维旋转问题,给定旋转轴和旋转角度,用罗德 ...
- 独立磁盘冗余阵列-RAID
一.RAID概述 RAID(Redundant Array of Independent Disks)即独立冗余磁盘阵列 磁盘阵列就是.由很多块廉价磁盘 组成的一个容量巨大的卷组.然后在使用不同级别的 ...
- Apache-Tomcat-Ajp漏洞(CVE-2020-1938)漏洞复现
前言 Apache Tomcat会开启AJP连接器,方便与其他Web服务器通过AJP协议进行交互.由于Tomcat本身也内含了HTTP服务器,因此也可以视作单独的Web服务器.此漏洞为文件包含漏洞,攻 ...
- 【Debian】 Debian 安装源配置
Debian 安装源配置 所有的Linux安装完后第一件事,就是要更新安装源 安装源是什么呢,安装源又称软件源,是指把软件的安装源地址放在一个pool里面,用一条命令(比如apt-get instal ...
- 软件bug描述(web)
1.bug编码与名称:测试日期+bug字段 2.测试环境:浏览器:全部/IE8,操作系统:win7 x64 3. 测试数据:用户名,密码,相关的业务账号 4.重现步骤:缺陷发现的过程 5. 缺陷说明: ...
- .NET面试题整理(持续更新)
1.已知值类型保存在线程栈中,引用类型保存在堆中 struct Point{ public int x,y; } public sealed class program{ public static ...