一些稍微优雅的技巧写法,并非非的是es6,标题党了哈
:class="{a: true, b: true}"
:class="['btn', 'btn2', {a: true, b: false}]"
if(flg === a || flg === b)
['a','b'].indexOf(flg) > -1 // 能够使用Array.prototype.includes() 返回 Boolean if(['a', 'b'].includes(flg)) {}
import a from './a.vue' componets: { a }
components: { a: require('./a.vue') }
<li v-for="(item,index) in data" :key="index"> {{item.uuid}} //输出uuid字段 </li>
<li v-for="{uuid} in data" :key="uuid"> {{uuid}} //直接解构赋值输出 </li>
设置比较长的class类名区分,或者使用BEN等命名方法
<style module> .h3 {} </style>
style样式会存在$style计算属性中javascript
//调用方式 <h3 :class="$style.h3"></h3> //$style是计算属性,因此也能够这样 bool为Bool表达式 <h3 :class="{$style.h3: bool}"></h3>
缺点: 生成一个独一无二的class类名,只能使用类名class控制样式
<style scoped> </style>
生成Hash属性标识.且根元素
受父组件
的scoped影响
使用
>>>
深度选择器
//寻找div下的样式,包括子组件样式 div >>> .h3 { }
对象尽可能静态化,一旦定义,就不得随意添加新的属性。若是添加属性不可避免,要使用Object.assign方法。
// bad const a = {}; a.x = 3; // if reshape unavoidable const a = {}; Object.assign(a, { x: 3 }); // good const a = { x: null }; a.x = 3;
若是对象的属性名是动态的,能够在创造对象的时候,使用属性表达式定义。
// bad const obj = { id: 5, name: 'San Francisco', }; obj[getKey('enabled')] = true; // good const obj = { id: 5, name: 'San Francisco', [getKey('enabled')]: true, //属性表达式 6 };
let arr1 = [1,2,3] let arr2 = [4,6,7] arr2 = arr2.concat(arr1)
let arr1 = [1,2,3] let arr2 = [...arr1,4,6,7]
var a = { key1: 1, key2: 2 } var b = Object.assign({}, a, { key3: 3 }) // 最后结果 {key1: 1, key2: 2,key3: 3 }
var a = { key1: 1, key2: 2 } var b = {...a, key3: 3}
使用reset paramete是纯粹的
Array
实例
function a() { console.log(arguments) } a(1,2,3)
function a(...args) { console.log(args) } a(1,2,3)
IE 任何系列都不支持
须要本身写工具函数css
var arr = [1,2,3] console.log(arr.includes(1)); // true console.log(arr.includes(4)); // false
IE 任何系列都不支持
var obj = { key1: 1, key2: 2, key3: 3 } Object.keys(obj); // ["key1", "key2", "key3"]
IE 任何系列都不支持
var obj = { key1: 1, key2: 2, key3: 3 } Object.values(obj); // [1,2,3]
适用于只有两三行简单操做vue
var x = 1; var y = 1 var z = x + y;
var z = (x=1, y = 1, x+ y)
当你想要在指望一个表达式的位置包含多个表达式时,能够使用逗号操做符