
<template>
<div>
<div class="block">
<span class="demonstration">默认 click 触发子菜单</span>
<el-cascader
v-model="value"
:options="newOptions"
@change="handleChange"
/>
</div>
</div>
</template>
<script>
import mixin from './mixin'
export default {
name: 'Home',
mixins: [mixin],
data() {
return {
value: [],
newOptions: []
}
},
mounted() {
this.filterOptions1(this.newOptions, this.options)
},
created() {
// this.filterOptions1(this.newOptions, this.options)
},
methods: {
handleChange() {
console.log('改变了')
},
filterOptions() {
const str = JSON.stringify(this.options)
console.log(str)
let str1 = str.replace(/(city|area)/g, (s) => {
return 'children'
})
str1 = str.replace(/name/g, (s) => {
return 'label'
})
console.log(str1)
},
// 想要的结构 [{label:'',value:'',children:[]}]
// 结构 [{name:'',city:[{name:'',area:['','','']}]}]
filterOptions1(newObj, obj) {
for (const key in obj) {
const label = obj[key]['name']
const citys = obj[key]['city']
const newCitys = []
for (const city in citys) {
const cityLabel = citys[city]['name']
const areas = citys[city]['area']
const newAreas = []
areas.forEach((item, index) => {
const newArea = { label: item, value: item }
newAreas.push(newArea)
})
const c = { label: cityLabel, value: cityLabel, children: newAreas }
newCitys.push(c)
}
newObj[key] = { label, value: label, children: newCitys }
}
}
}
}
</script>
<style>
</style>