Vue 和 React 语法对比 & polyfill

1. class 类名+布尔值

// vue
<div v-bind:class="{ active: isActive }"></div>
//react + classnames  [https://www.jianshu.com/p/9cf57787360d]
render(){
  var btnClass = classNames({
      'btn': true,
      'btn-pressed': this.state.isPressed,
      'btn-over': !this.state.isPressed && this.state.isHovered
    });
  return <button className={btnClass}>{this.props.label}</button>;
}

2. css 样式作用域

vue

<!-- vue 转义前 -->
<style lang="scss" scoped>
    .test {
        background: blue;
        span{
            color:red;
        }
    }
</style>
<template>
    <div class="test">
        <span>hello world !</span>
    </div>
</template>
<!-- vue 转义后 -->
<style lang="css">
    .test[data-v-ff86ae42] {
        background: blue;
    }
    .test span[data-v-ff86ae42]{
        color: red;
    }
</style>
<template>
    <div class="test" data-v-ff86ae42>
        <span data-v-ff86ae42>hello world !</span>
    </div>
</template>
<!-- 穿透写法 -->
<template>
  <div class="box">
    <dialog></dialog>
  </div>
</template>
<!--使用 >>> 或者 /deep/ 操作符(Sass 之类的预处理器无法正确解析 >>>,可以使用/deep/)-->
<style lang="scss" scoped>
.box >>> input {
    width: 166px;
    text-align: center;
  }
}
</style>

react + css-modules

// webpack.config.js
modules.exports = {
module: {
  loaders: [
    // ...
    {
      test: /\.css$/,
      loader: "style-loader!css-loader?modules&localIdentName=[path][name]---[local]---[hash:base64:5]"
    },
  ]
}}
// .css
.title {
  color: red;
}
:global(.title) {
  color: green;
}
// .tsx
import React from 'react';
import style from './App.css';

export default () => {
  return (
    <h1 className={style.title}>
      Hello World
    </h1>
  );
};
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • vue对比react、Angular(转官方文档) React React 和 Vue 有许多相似之处,它们都有:...
    simple_50a1阅读 1,379评论 0 5
  • 前端开发面试题 面试题目: 根据你的等级和职位的变化,入门级到专家级,广度和深度都会有所增加。 题目类型: 理论知...
    怡宝丶阅读 2,609评论 0 7
  • Vue对比其他框架 这个页面无疑是最难编写的,但我们认为它也是非常重要的。或许你曾遇到了一些问题并且已经用其他的框...
    伊滴墨阅读 1,822评论 0 15
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,314评论 0 3
  • Vue也已经升级到2.0版本了,到现在为止(2016/11/19)比较流行的MVVM框架有AngularJS(也有...
    彬_仔阅读 27,259评论 12 114