eval() 可将字符串与变量名拼接后再转变为变量
<el-button text class="expandOrFold" @click="handleExpandOrFold('Pre', pre_handlers.length)">
一键展开/折叠
</el-button>
<el-button text class="expandOrFold" @click="handleExpandOrFold('Post', post_handlers.length)">
一键展开/折叠
</el-button>
const activeNamesPre = ref(["0"]);
const activeNamesPost = ref(["0"]);
// 一键展开/折叠
const handleExpandOrFold = (str: any, length: number) => {
// eval()字符串与变量名拼接转变为变量
// 如str传入'Pre',eval("activeNames" + str)拼接得到activeNamesPre的变量名
if (eval("activeNames" + str).value.length > 0) {
eval("activeNames" + str).value = [];
} else {
eval("activeNames" + str).value = [];
for (let i = 0; i <= length - 1; i++) {
eval("activeNames" + str).value.push(String(i));
}
}
};