list删除元素
使用splice方法删除下标为index的元素
this.splice(index, 1);
list代码循环
使用foreach
res.forEach((item, key) => {console.log(item);});
使用fori
for (let i = 0, len = list.length; i < len; i++) {console.log(list[i]);}
list动态添加
this.lists.push({id:1,title:'abc'})
vue跳转界面
this.$router.push('/home/first')this.$router.push({path: '/home/first' })this.$router.push({path: '/order/page1',query:{ id:'2'}});this.$router.push({name: '/order/page2',params:{ id:'6'}});
string转int
var j = parseInt(“字符串”); //j 就是数值类型了
删除对象中某个属性
this.$delete(this.form,'members')//删除form对象中的members属性
动态给对象添加属性
this.$set(this.pushJson, 'alias', []);//给pushJson对象添加属性alias并赋值为空数组this.$set(this.pushJson, 'num', 1);//给pushJson对象添加属性num并赋值为1
时间转换
格林威治时间格式GMT —> 普通时间格式:
function GMTToStr(time){var date = new Date(time)var Str=date.getFullYear() + '-' +(date.getMonth() + 1) + '-' +date.getDate() + ' ' +date.getHours() + ':' +date.getMinutes() + ':' +date.getSeconds()return Str}
普通时间格式 —> GMT:
function StrToGMT(time){var GMT = new Date(time)return GMT}
RSA加密
npm i jsencryptimport JsEncrypt from 'jsencrypt/bin/jsencrypt';let jse = new JsEncrypt();jse.setPublicKey(pubKey); // 加入rsa public keylet password = jse.encrypt(password); // 将password加密
设置cookie、获取cookie
//设置cookiesetCookie: function (cname, cvalue, exdays) {var d = new Date();d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));var expires = "expires=" + d.toUTCString();console.info(cname + "=" + cvalue + "; " + expires);document.cookie = cname + "=" + cvalue + "; " + expires;console.info(document.cookie);},//获取cookiegetCookie: function (cname) {var name = cname + "=";var ca = document.cookie.split(';');console.log("获取cookie,现在循环")for (var i = 0; i < ca.length; i++) {var c = ca[i];console.log(c)while (c.charAt(0) == ' ') c = c.substring(1);if (c.indexOf(name) != -1){return c.substring(name.length, c.length);}}return "";},//清除cookieclearCookie: function () {this.setCookie("username", "", -1);},checkCookie: function () {var user = this.getCookie("username");if (user != "") {alert("Welcome again " + user);} else {user = prompt("Please enter your name:", "");if (user != "" && user != null) {this.setCookie("username", user, 365);}}}
获取当前页面的url,获取路由地址
window.location.href // 完整urlthis.$route.params //路由路径参数 例如:/user/:id → /user/2044011030 → this.$route.params.idthis.$route.path //当前路由对象的路径,如'/vithis.$route.query //请求参数,如/foo?user=1获取到user = 1this.$route.router //所属路由器以及所属组件信息this.$route.matched //数组,包含当前匹配的路径中所包含的所有片段所对应的配置参数对象。this.$route.name //当前路径名字
在vue路由器中获取所有路由
$router.options.routes
axios 请求带上cookie
import axios from 'axios'// 全局设置 axios 发送请求带上cookieaxios.defaults.withCredentials = true
加入 withCredentials 后无法跨域
服务端要做以下2个工作:
- Access-Control-Allow-Origin字段必须指定域名,不能为*
- Access-Control-Allow-Credentials为true
vue中用watch监听当前路由
watch:{$route:{handler(newRouter){this.curTagName=newRouter.name;},immediate: true}},
恢复数据为初始状态
this.data:获取当前状态下的data,也就是要改变的data数据;Object.assign(this.$data.changePassParams, this.$options.data().changePassParams)
this.options.data(): vue原始的数据,就是你页面刚加载时的 datathis.$nextTick()的用法
将回调延迟到下次DOM更新循环之后执行。在修改数据之后立即使用它,然后等待DOM更新。
