(感觉写麻烦了,先记录一下)
原代码,a.vue文件
data() {
return {
newTablist: [
{
label: '文本1',
value: '0',
list: [
{ text: '1文本0', number: "-" ,type: 'fir10'},
{ text: '1文本1', number: "-",type: 'fir11' },
{ text: '1文本2', number: "-" ,type: 'fir12'}
]
},
{
label: '文本2',
value: '1',
list:[
{ text: '2文本0', number: "-" ,type: 'sec20'},
{ text: '2文本1', number: "-" ,type: 'sec21'},
{ text: '2文本2', number: "-" ,type: 'sec22'},
{ text: '2文本3', number: "-" ,type: 'sec23'},
{ text: '2文本4', number: "-" ,type: 'sec24'}
]
},
{
label: '文本3',
value: '2',
list:[
{ text: '3文本0', number: "-" ,type: 'th30'},
{ text: '3文本1', number: "-" ,type: 'th31'},
{ text: '3文本2', number: "-" ,type: 'th32'},
{ text: '3文本3', number: "-" ,type: 'th33'},
]
},
],
};
}
优化代码,test.js文件
export const TEXT0 = {
fir10: '1文本0',
fir11: '1文本1',
fir12: '1文本2',
};
export const TEXT1 = {
sec20: '2文本0',
sec21: '2文本1',
sec22: '2文本2',
sec23: '2文本3',
sec24: '2文本4',
};
export const TEXT2 = {
th30: '3文本0',
th31: '3文本1',
th32: '3文本2',
th33: '3文本3',
};
export function getList(id) {
let list = []; let indata;
switch (id) {
case '0':
indata = TEXT0
break;
case '1':
indata = TEXT1
break;
case '2':
indata = TEXT2
break;
}
for(let key in indata){
list.push({ text:indata[key], number: "-", type:key })
}
return list;
}
a.vue文件引入test.js文件
import {getList} from './test';
data() {
return {
newTablist: [
{
label: '文本1',
value: '0',
list: getList('0')
},
{
label: '文本2',
value: '1',
list:getList('1')
},
{
label: '文本3',
value: '2',
list:getList('2')
},
],
};
}