對(duì)象obj,屬性key,設(shè)置value
this.$set(obj,key,value)
數(shù)組arr,索引index,設(shè)置value
this.$set(arr,index,value)
其他奇葩問(wèn)題的首要思路
(1)屬性未聲明
(2)返回列表內(nèi)無(wú)此屬性
(3)屬性類型錯(cuò)誤 '',[],new Map()
(4)值為0 的時(shí)候 的判斷
JSON.parse(JSON.stringify(obj);
如 this.form = res.data;
console.log(this.form, res.data);
會(huì)發(fā)現(xiàn)res.data會(huì)受this.form影響,類型會(huì)隨之變化
typeof index === 'undefined' || index===null || index ==="" || index.length === 0
getAction(url,params).then(async (res) => {
await this.xxfunction();
});
async xxfunction(){
}
以下為global.js內(nèi)容
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頁(yè)面內(nèi)如何使用
this._GLOBAL.yesOrNoTableData
數(shù)組去重
uniqueValArray(array) {
return array.filter(function(ele, index, array) {
return array.indexOf(ele) === index;
});
}
獲取數(shù)組內(nèi)某組元素,輸出指定屬性元素
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;
}