ES6常用语法

变量的定义

let定义变量

  • 不会变量提升

  • 有全局作用域和函数作用域,块级作用域{}

  • 不能重复定义

var定义变量

  • 会变量提升

  • 只有全局作用域和函数作用域

  • 能够重复定义

const定义变量

  • 定义的是一个 常量,定义之后不能进行修改

  • 没有变量提升

  • 不能重复定义

  • 带来了块级作用域

  • 定义常量的时候必须赋值

模板字符串

let username1 = "username"

oDiv.innerHTML = `

<h1>${username1}</h1>

<h2></h2>

`

用反引号进行字符串的拼接

用${}来存储变量

数据的解构和赋值

let ary = [1,2,3]

解构:

let [a, b, c] = ary

let obj = {

username: 'xxx',

age:23

}

let {username, age}= obj; # 没有赋值是默认两者相同

let {username:新的名字, age:新的名字}= obj;

  • 解构的左右两端为相同的数据类型

let a=1;

let b=2;

[a, b] = [b, a]

  • 交换数据的赋值需要放入数组中

函数的扩展

function foo(x, y){

let number = y || 10;

return number

}

foo(1, 2)

foo(1)

foo(1, 0)

function foo(x, y=10){}

  • ES6有了默认值参数

  • 箭头函数

    • 一个参数

    • v => v;

      let foo = v => v;
      ret = foo(10);
      console.log(ret); 10
    • 0和或者多个参数

    • let bar = (x, y)=>{return x + y;}

  • 箭头函数的使用规则

    • 定义的时候在什么地方,将来就不会改变了

    • 箭头函数的this指向定义时的作用域

    • 普通函数的this指向调用者的作用域

类的扩展

<script>
# ES5
function Person(){
      this.username = 'xxx';
      this.age = 20;
}
Person.prototype.showInfo = function(){
    console.log(this.username);  
}
let xxx = new Person();
</script>
ES6

class Person{
  constructor (username, age, mind){
      this.username = username;
      this.age = age;
      this.mind = mind;
  }
  // 直接写方法不需要逗号
  showInfo(){
      console.log(this.username, this.age. this.mind)
  }
}
let xxx = new Person('xxx', 18, '000);
xxx.showInfo()
  • class关键字定义一个类

    • 必须有constructor方法(构造方法,如果没有,constructor{}

    • 必须使用new来实例化,否则报错

  • 类的继承

    • 必须在子类的constructor方法中写super方法


class Peiqi extends Person{
constructor (username, age){
super()
      this.username = username;
      this.age = age;
  }    
}

let peiqi = new Peiqi('peiqi', 73)
peiqi.shoeInfo() # 继承了父类

模块化编程

对象的单体模式

let obj = {
  naem : 'xxx'.
  foo(){
      console.log(this.name);
  }
}
obj.foo() # xxx
  • 解决了箭头函数的this指向问题

Vue.js

为什么用框架

// 数据模板引擎       ----   渐进式的框架
// v-开头的指令是帮助我们渲染数据用的
<div id='app'> {{ greeting }} </div>

new Vue({
  el: '#app',
  data: {
      greeting : 'hello Vue',
  }
})

Vue之常用指令(用来渲染数据)

{{ greeting }}
v-text
<div id='app' v-text='greeting'></div>

new Vue({ // 根实例
  el: '#app',
  data: {
      greeting : 'hello Vue',
  }
})
v-html
<div id='app' v-html='greeting'></div>

new Vue({ // 根实例
  el: '#app',
  data: {
      greeting : '<h1>hello Vue</h1>',
  }
})
v-for
<div id='app'>
<ul>
<li v-for="aihao in hobby">{{aihao}}</li>
</ul>
<ul>
<li v-for="student in students">
姓名:{{student.name}}
年龄:{{student.age}}
爱好:{{student.hobby}}
</li>
</ul>
</div>

new Vue({ // 根实例
  el: '#app',
  data: {
      hobby: ['抽烟','喝酒','烫头'],
      students: [
          {
              name: '张',
              age: 12,
              hobby: 'girls',
          },
          {
              name: '李',
              age: 18,
              hobby: 'boys'
          },
          {
              name: '王',
              age: 90.
              hobby: '无',
          }
      ]
  }
})
v-if
<div id='app'>
<div v-if="role == 'girl'">
       <h1>
          女的
       </h1>
   </div>
   <div v-else-if="role == 'boy'">
       <h1>
          男的
       </h1>
   </div>
   <div v-else>
       <h1>
          不男不女
       </h1>
   </div>
</div>

<script>
   let vm = new Vue({
       el: '#app',
       data: {
           role: "girl"
      }
  })
</script>
v-show
<div id='app'>
<div v-show="isShow">Hello Word</div>
</div>

<script>
   let vm = new Vue({
       el: '#app',
       data: {
           isShow: true
      }
  })
</script>
  • v-开头的指令是帮助我们渲染数据用的

  • v-show切换性能高一些,用display控制的

  • v-if的切换是同append控制的,慢

  • 加载性能v-show的慢v-if加载的快

v-bind //绑定
<style>
   .active {
       width: 10px;
       height: 10px;
       background-color: black;
  }
</style>
<div id='app'>
<a v-bind:href="jingdong">去京东</a>
  <div v-bind:class="{ active: isActive,..., }"></div>
   <div v-bind:class="[ isActive,..., ]"></div>
</div>

<script>
   let vm = new Vue({
       el: '#app',
       data: {
           jingdong: "www.jd.com",
           isActive: true,  // 1
           isActive: 'active',
      }
  })
</script>
v-on
<style>
   .active {
       width: 10px;
       height: 10px;
       background-color: black;
  }
</style>
<div id='app'>
<h1 :class="{active: isActive}">
      sao
   </h1>
  // 简写<button @:click="changeColor">
   <button v-on:click="changeColor">
      点击变色
   </button>
</div>

<script>
   let vm = new Vue({
       el: '#app',
       data: {
           isShow: false
      }.
       methods:{
       changeColor: function(){
      this.isAcative = !this.isActive;
  },
       changeColor1: function(){},        
  }
  })
</script>
v-model         ----   双向数据绑定

<div id='app'>
<input v-model="name" type="text">
  <input type="chackbox" value="men" v-model="genders">
   <input type="chackbox" value="women" v-model="genders">
   <select v-model="girls">
       <option>女友1</option>
       <option>女友2</option>
       <option>女友3</option>
   </select>
   <textarea></textarea>
   <hr>
  {{name}}
  {{genders}}
  {{girls}}
</div>

<script>
   let vm = new Vue({
       el: '#app',
       data: {
           name: 'xxx',
           genders: [],
           girls: [],
      }
  })
</script>

指令修饰符

<div id='app'>
<table border="1">
       <thead>
      <tr>
          <th>学科</th>
               <th>成绩</th>
           </tr>
       </thead>
      <tr>
               <td>python</td>             // 修饰符
               <td><input type="text" v-model.number="python"></td>
      </tr>
      <tr>
               <td>Vue</td> // 修饰符(失去焦点后计算)
               <td><input type="text" v-model.lazy="vue"></td>
      </tr>
      <tr>
               <td>Go</td> // 去左右空格
               <td><input type="text" v-model.trim="go"></td>
      </tr>
      <tr>
               <td>总成绩</td>
              // 不合适,引入了计算属性
              //<td>{{python + vue + go}}</td>
               <td>{{sumScore}}</td>
      </tr>
       <tbody>        </tbody>
   </table>
</div>

<script>
   let vm = new Vue({
       el: '#app',
       data: {
           python: 98,
           vue: 100,
           go: 90,
      },
       // 计算属性
       computed: {
           // 页面刷新的时候就渲染了
           sumScore: function(){
               return this.python+this.vue+this.go
          }
      },
       // 侦听属性
       // 解耦 - 监听
       watch: {
           // 数据变动的时候才执行,不会刷新页面的时候执行
           python: function(){
               // 没有return的,没有接收值
               return this.python + 1
               // 进行一些逻辑操作
               alert("python 被修改")
          }
      }
       
  })
</script>
.number
.lazy

获取DOM元素

ref="名字"
<style>
   .active {
       width: 10px;
       height: 10px;
       background-color: black;
  }
</style>
<div id='app'>
  // 国定格式
<div ref="myRef">peiqi</div>
  // 简写<button @:click="changeColor">
   <button v-on:click="changeColor">
      点击变色
   </button>
</div>

<script>
   let vm = new Vue({
       el: '#app',
       data: {
           
      },
       methods:{
       changeColor: function(){
          // this.$refs is a object
          // this.$refs.myRef就是那个标签
      this.$refs.myRef.style.color = 'red';
      this.isAcative = !this.isActive;
  },
  }
  })
</script>

自定义指令

<style>
   .box {
       width: 100px;
       height: 100px;
       background-color: red;
  }
</style>
<div id='app' v-bind:class="{box: isShow}">
   <div v-pos="position">Hello Vue</div>
</div>

<script>
   // 自定义指令
   Vue.directive("pos", function(el, bindding){
       // el标签   bindding绑定的数据
       if (bindding.value){
           el.style['position'] = 'fixed';
           el.style['right'] = 0;
           el.style['bottom'] = 0;
      }
  })
   let vm = new Vue({
       el: '#app',
       data: {
           position: true,
           isShow: true,
      },
  })
</script>
还有一种方法
<div id="app">
      <div v-bind:class="{ box: isShow}" v-pos.right.bottom="leftBottom">Hello Vue!</div>
  </div>

  <script>
      // 数据模板引擎
      // v-开头的指令是帮助我们渲染数据用的

      Vue.directive("pos", function (el, bindding) {
          console.log("el: ", el);
          console.log("bindding: ", bindding);
          if (bindding.value) {
              el.style['position'] = 'fixed';
              for (let key in bindding.modifiers) {
                  el.style[key] = 0;
              }
              // el.style['right'] = 0;
              // el.style['bottom'] = 0;
          }
      });

      let vm = new Vue({
          el: "#app",
          data: {
              leftBottom: true,
              isShow: true,
          },
      })
  </script>

Vue知识点小总结1的更多相关文章

  1. Vue + WebApi 小项目:构造自己的在线 Markdown 笔记本应用

    Vue + WebApi 小项目:构造自己的在线 Markdown 笔记本应用 目录 概要 知识点 完整示例图 代码与资源文件 流程步骤 概要 基于 MVP 最小可行性产品设计理念,我们先完成一个可以 ...

  2. 【转】HTML5的小知识点小集合

    html5的小知识点小集合 html5知识   1.  Doctype作用?标准模式与兼容模式各有什么区别? (1).<!DOCTYPE>声明位于位于HTML文档中的第一行,处于<h ...

  3. html5的小知识点小集合

      html5的小知识点小集合 html5知识   1.  Doctype作用?标准模式与兼容模式各有什么区别? (1).<!DOCTYPE>声明位于位于HTML文档中的第一行,处于< ...

  4. Vue之小入门

    Vue之小入门 <div id="app">{{ greeting }}</div> <script> let oDiv = document. ...

  5. 【vue知识点】1)vue生命周期

    [vue知识点]2)vue登录认证

  6. 跟我一起做一个vue的小项目(二)

    这个vue项目是紧跟着之前的项目跟我一起做一个vue的小项目(一)来的. 我继续后面的开发(写的比较粗糙,边学边记录) 下图是header头部的样式 header组件内容如下 //header.vue ...

  7. vue图书小案例

    小知识点: vue中计算属性有缓存(对象属性变化时才会更新),方法没有缓存,所以计算属性比方法效率高js中let有块级作用域,var没有块级作用域,所以var是有缺陷的this.letters[0] ...

  8. 基于Vue的小日历(支持按周切换)

      基于Vue的日历小功能,可根据实际开发情况按每年.每月.每周.进行切换 <template> <div class="date"> <!-- 年份 ...

  9. vue.js小总结

    Vue.js 的核心是一个允许采用简洁的模板语法来声明式地将数据渲染进 DOM 的系统; 指令带有前缀 v-,以表示它们是 Vue 提供的特殊特性; v-for 指令可以绑定数组的数据来渲染一个项目列 ...

随机推荐

  1. 【IntelliJ 】IntelliJ IDEA 自动导入包 快捷方式 关闭重复代码提示

    idea可以自动优化导入包,但是有多个同名的类调用不同的包,必须自己手动Alt+Enter设置 设置idea导入包 勾选标注 1 选项,IntelliJ IDEA 将在我们书写代码的时候自动帮我们优化 ...

  2. csu - 1537: Miscalculation (模拟题)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1537 因为给出的式子是必定合法的,只要用两个栈分别保存符号和数字.算出答案后和从左至右算的答案比对 ...

  3. 博弈论入门题 kiki's game

    Problem Description Recently kiki has nothing to do. While she is bored, an idea appears in his mind ...

  4. 创建Django项目(七)——表单

    2013-08-15 19:43:01|         1.URL配置和视图 "blog\urls.py"文件中:添加url(r'write_article/$', 'write ...

  5. [bzoj3160]万径人踪灭_FFT_Manacher

    万径人踪灭 bzoj-3160 题目大意:给定一个ab串.求所有的子序列满足:位置和字符都关于某条对称轴对称而且不连续. 注释:$1\le n\le 10^5$. 想法: 看了大爷的题解,OrzOrz ...

  6. [bzoj3712][PA2014]Fiolki_倍增LCA

    Fiolki bzoj-3712 PA-2014 题目大意:题目链接. 注释:略. 想法: 神题! 我们建树:对于一次倾倒操作,我们弄一个新的大瓶子作为两个合并瓶子的父亲节点,与两个瓶子相连. 对于一 ...

  7. poj——1422 Air Raid

    Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8577   Accepted: 5127 Descript ...

  8. SaltStack学习系列之State安装Nginx+PHP环境

    目录结构 |-- pillar | |-- nginx | | `-- nginx.sls #nginx变量(key:value) | `-- top.sls `-- salt|-- init #初始 ...

  9. Centos 备份 还原

    備份: tar cvpzf backup.tgz / --exclude=/backup.tgz --exclude=/mnt 記得一定要排除備份文件本身哦! 還原: tar xvpfz backup ...

  10. VB.NET+三层 机房收费系统之组合查询

    关系组合查询已经用去了4天的时间.每天都在痛苦中煎熬,绞尽脑汁,一句代码都要瞪大眼睛看好长时间,有时候.由于两句话颠倒了.就nothing了:有时候,由于table如何可以转换成实体类型.将自己困住了 ...