对象obj,属性key,设置value
this.$set(obj,key,value)
数组arr,索引index,设置value
this.$set(arr,index,value)
其他奇葩问题的首要思路
(1)属性未声明
(2)返回列表内无此属性
(3)属性类型错误 '',[],new Map()
(4)值为0 的时候 的判断
JSON.parse(JSON.stringify(obj);
如 this.form = res.data;
console.log(this.form, res.data);
会发现res.data会受this.form影响,类型会随之变化
typeof index === 'undefined' || index===null || index ==="" || index.length === 0
getAction(url,params).then(async (res) => {
await this.xxfunction();
});
async xxfunction(){
}
以下为global.js内容
const yesOrNoTableData=[
{"value":1,"label":"是"},
{"value":0,"label":"否"},
];
const changeObjectToLabel = (dataOption, data, prop) =>{
if(typeof data === "object"){
return changeValueToLabel(dataOption, data[prop]);
}
};
export default
{
yesOrNoTableData,
changeObjectToLabel,
};
修改main.js
import _GLOBAL from "@/api/global/global";
Vue.prototype._GLOBAL = _GLOBAL;
vue页面内如何使用
this._GLOBAL.yesOrNoTableData
数组去重
uniqueValArray(array) {
return array.filter(function(ele, index, array) {
return array.indexOf(ele) === index;
});
}
获取数组内某组元素,输出指定属性元素
convertToSelect(array, sort, direction, ivalue, itext, ovalue, otext) {
let options = [];
if (array == null || array.length === 0) {
return options;
}
if (sort && direction) {
if (direction === "asc") {
array = array.sort(function compareFunction(param1, param2) {
return param1[sort].localeCompare(param2[sort]);
});
} else {
array = array.sort(function compareFunction(param1, param2) {
return param2[sort].localeCompare(param1[sort]);
});
}
}
for (let i = 0; i < array.length; i++) {
let option = {};
option[ovalue] = array[i][ivalue];
option[otext] = array[i][itext];
options.push(option);
}
return options;
}