vue2 手记

vue2 手记

Vue文档:https://cn.vuejs.org/v2/api/#provide-inject

Vue 生命周期:https://cn.vuejs.org/v2/guide/instance.html

vuex状态管理:https://vuex.vuejs.org/zh/

1.简单循环

2.数组包含某值函数.includes()

3.获取路由

4. 获取数据类型

5.数据克隆

1.简单循环

1).

item:定义的每一条的变量

response.data.result:要循环的数组

for(let item of response.data.result) {
    //用item操作每一条数据。
}

2).

response.data.result:要循环的数组

index:索引

response.data.result.forEach((item, index) => {
    // 用item操作每一条数据。
})

  

2.数组包含某值函数.includes()

[1, 2, 3].includes(2); // true
 
[1, 2, 3].includes(4); // false

[1, 2, 3].includes(3, 3); // false
 
[1, 2, 3].includes(3, -1); // true

 详细:https://www.cnblogs.com/rain-in-summer/p/9792165.html

3.获取路由

window.document.location.pathname // 亲测

this.$route.query

router.split('?')[1]

  

4. 获取数据类型

 1)typeof 

typeof data // 获取数据类型(array,object)

  或

typeof(a) //旧写法

 2)isNaN

使用js自带全局函数isNaN(), isNaN()返回一个Boolean值,如下 :

var c="hello"; //字符串

isNaN(c); //返回一个true;

var c=10; //数字

isNaN(c);//返回一个false

如果以上c为一个空串或是一个空格,isNaN将把c当作数字0来处理,所以检查不严谨。

3)正则表达式

function checkNum(input){

var reg=/^[0-9]+.?[0-9]*$/; //判断字符串是否为数字 ,判断正整数用/^[1-9]+[0-9]*]*$/

var num=document.getElementById(input).value;

if(!reg.test(num)){

        alert("请输入数字");

    document.getElementById(input).value="";

    return false;

    }

}

  

5.数据克隆

1)

objClone=JSON.parse(JSON.stringify(obj))

2)

function deepClone (data) {
  let type = typeof data
  let obj
  if (type === 'array') {
    obj = []
  } else if (type === 'object') {
    obj = {}
  } else {
    // 不再具有下一层次
    return data
  }
  if (type === 'array') {
    for (let i = 0, len = data.length; i < len; i++) {
      obj.push(deepClone(data[i]))
    }
  } else if (type === 'object') {
    for (const key in data) {
      if (key == 0) {
        obj = []
      }
      if (!isNaN(key)) {
        obj.push(deepClone(data[key]))
      } else {
        obj[key] = deepClone(data[key])
      }
    }
  }
  return obj
}

  

JS使用Cookie:https://www.cnblogs.com/cxscode/p/11177580.html

Vue2技术栈归纳与精粹:https://blog.csdn.net/sinat_17775997/article/details/78913968

原文地址:https://www.cnblogs.com/cxscode/p/11177150.html