转换vue3 写法 data () { return { title: '市南区', value: 9323, topData: [], filterData: [], navTop: [], // 名字行政区划 xsource: [], // 值 centerWidth: '', // 外面柱状图的 show: true, centerHeight: '' } }, computed: { defaultOption () { return { step: 0.5 // 数值越大速度滚动越快 } } }, props: { option: { type: Object, default: () => { return [] } } }, created () { this.$ref.scroll3._startMove() }, destroyed () { this.$refs.scroll3._cancle() }, watch: { option: { immediate: true, deep: true, handler: function (newValue, oldValue) { this.show = true if (!newValue.基础配置.swiper) { this.show = false } this.filterData = newValue.数据.source this.navTop = newValue.数据配置.xDimensions // name 行政区划 this.xsource = newValue.数据配置.yDimensions this.centerWidth = newValue.基础配置.centerWidth this.centerHeight = newValue.基础配置.centerHeight this.topHeight = newValue.基础配置.topHeight let maxValue = 0 this.filterData.forEach((item, index) => { if (Number(item[this.xsource[0].name]) > maxValue) { maxValue = item[this.xsource[0].name] } }) this.filterData.forEach((item, index) => { if (item[this.xsource[0].name] == maxValue) { item.width = 100 } else { item.width = item[this.xsource[0].name] / maxValue * 100 } if (newValue.基础配置.leftBackground) { item.leftBackground = newValue.基础配置.leftBackground } else { item.leftBackground = 'linear-gradient(90deg, #2F61C2 0%, #60C0E8 100%)' } }) // 最大的值 let max = [] for (var i = 0; i < this.filterData.length; i++) { for (var j = i; j < this.filterData.length; j++) { if (Number(this.filterData[i][this.xsource[0].name]) < Number(this.filterData[j][this.xsource[0].name])) { max = this.filterData[j] this.filterData[j] = this.filterData[i] this.filterData[i] = max } } } } } }
时间: 2024-02-14 09:18:32 浏览: 108
在Vue3中,可以将data、computed、props、created、destroyed和watch等属性直接写在setup函数中,代码如下:
```
import { ref, watch } from 'vue'
export default {
setup(props) {
const title = ref('市南区')
const value = ref(9323)
const topData = ref([])
const filterData = ref([])
const navTop = ref([])
const xsource = ref([])
const centerWidth = ref('')
const show = ref(true)
const centerHeight = ref('')
const topHeight = ref('')
const defaultOption = ref({
step: 0.5
})
const startMove = () => {
// TODO
}
const cancle = () => {
// TODO
}
watch(props.option, (newValue, oldValue) => {
show.value = true
if (!newValue.基础配置.swiper) {
show.value = false
}
filterData.value = newValue.数据.source
navTop.value = newValue.数据配置.xDimensions // name 行政区划
xsource.value = newValue.数据配置.yDimensions
centerWidth.value = newValue.基础配置.centerWidth
centerHeight.value = newValue.基础配置.centerHeight
topHeight.value = newValue.基础配置.topHeight
let maxValue = 0
filterData.value.forEach((item, index) => {
if (Number(item[xsource.value[0].name]) > maxValue) {
maxValue = item[xsource.value[0].name]
}
})
filterData.value.forEach((item, index) => {
if (item[xsource.value[0].name] == maxValue) {
item.width = 100
} else {
item.width = item[xsource.value[0].name] / maxValue * 100
}
if (newValue.基础配置.leftBackground) {
item.leftBackground = newValue.基础配置.leftBackground
} else {
item.leftBackground = 'linear-gradient(90deg, #2F61C2 0%, #60C0E8 100%)'
}
})
// 最大的值
let max = []
for (var i = 0; i < filterData.value.length; i++) {
for (var j = i; j < filterData.value.length; j++) {
if (Number(filterData.value[i][xsource.value[0].name]) < Number(filterData.value[j][xsource.value[0].name])) {
max = filterData.value[j]
filterData.value[j] = filterData.value[i]
filterData.value[i] = max
}
}
}
}, { immediate: true, deep: true })
return {
title,
value,
topData,
filterData,
navTop,
xsource,
centerWidth,
show,
centerHeight,
topHeight,
defaultOption,
startMove,
cancle
}
}
}
```
阅读全文