<script setup> import { ref,reactive,onMounted ,computed,onUpdated} from 'vue' import * as echarts from 'echarts'; import bootstrap from 'bootstrap/dist/js/bootstrap.js' let isfull=ref() let ismin=ref() let midSize=1080 let windowWidth=ref(window.innerWidth) let windowHeight=ref(window.innerHeight) function pageinit(){ getWindowSize(); if(windowWidth.value>midSize){ isfull.value=true ; ismin.value=false; }else{ isfull.value=false ; ismin.value=true; } } function getWindowSize() { windowWidth.value = window.innerWidth; windowHeight.value = window.innerHeight; } onUpdated(()=>{ rizeChart(OutPutChart) rizeChart(ObjChart) }) onMounted(()=>{ pageinit(); let OutPutOption=({ xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, yAxis: { type: 'value' }, series: [ { data: [820, 932, 901, 934, 1290, 1330, 1320], type: 'line', smooth: true } ] }; let OutPutChart= echarts.init(document.getElementById('OutPutChart')) OutPutOption && OutPutChart.setOption(OutPutOption) rizeChart(OutPutChart) window.addEventListener('resize', function() { pageinit() rizeChart(OutPutChart) }) }) function rizeChart(chart){ chart.resize; } const card_size = computed(() => { return isfull.value ? 'card_size_full' : 'card_size_min' }) </script> <template> <div> <div class="card " :class="card_size" > <div class="card-header card-color"> 成品数据报表 </div> <div class="card-body"> <div ref="OutPutChart" id="OutPutChart" style="width: 100%;height: 400px; margin: 0 auto;"></div> </div> </div> </div> </template> <style scoped> .card-color{ text-align: center !important; background-color: #ffffff !important; } .card_size_full{ width: 49%; height: 450px; margin-right: 10px; margin-bottom: 10px; float: left; } .card_size_min{ width: 99%; height: 450px; margin: 5px auto; } </style>我的代码是这样的能帮我改一下吗
时间: 2024-04-17 20:26:52 浏览: 237
当你在Vue组件中使用`<script setup>`语法时,无法直接使用`onMounted`和`onUpdated`等钩子函数。你可以使用`onBeforeMount`和`onUpdated`代替它们。
此外,你在调用`chart.resize()`方法时,需要加上括号,即`chart.resize()`。
以下是根据你提供的代码进行的修改:
```vue
<script setup>
import { ref, onBeforeMount, onUpdated, computed } from 'vue'
import * as echarts from 'echarts';
let isfull = ref(false);
let ismin = ref(true);
let midSize = 1080;
let windowWidth = ref(window.innerWidth);
let windowHeight = ref(window.innerHeight);
function pageinit() {
getWindowSize();
if (windowWidth.value > midSize) {
isfull.value = true;
ismin.value = false;
} else {
isfull.value = false;
ismin.value = true;
}
}
function getWindowSize() {
windowWidth.value = window.innerWidth;
windowHeight.value = window.innerHeight;
}
onUpdated(() => {
resizeChart(OutPutChart);
resizeChart(ObjChart);
})
onBeforeMount(() => {
pageinit();
let OutPutOption = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
data: [820, 932, 901, 934, 1290, 1330, 1320],
type: 'line',
smooth: true
}
]
};
let OutPutChart = null;
onBeforeMount(() => {
OutPutChart = echarts.init(document.getElementById('OutPutChart'));
OutPutChart.setOption(OutPutOption);
resizeChart(OutPutChart);
})
function resizeChart(chart) {
chart.resize();
}
window.addEventListener('resize', () => {
pageinit();
resizeChart(OutPutChart);
})
})
const card_size = computed(() => {
return isfull.value ? 'card_size_full' : 'card_size_min';
})
</script>
<template>
<div>
<div class="card" :class="card_size">
<div class="card-header card-color">
成品数据报表
</div>
<div class="card-body">
<div ref="OutPutChart" id="OutPutChart" style="width: 100%; height: 400px; margin: 0 auto;"></div>
</div>
</div>
</div>
</template>
<style scoped>
.card-color {
text-align: center !important;
background-color: #ffffff !important;
}
.card_size_full {
width: 49%;
height: 450px;
margin-right: 10px;
margin-bottom: 10px;
float: left;
}
.card_size_min {
width: 99%;
height: 450px;
margin: 5px auto;
}
</style>
```
这些修改包括修正钩子函数的使用,调整了代码的结构,并将`OutPutChart`的声明放在了正确的位置。希望这可以解决你的问题。如果还有其他问题,请随时提问。
阅读全文