【4.0】vue3中的属性

【4.0】vue3中的属性

【一】计算属性computed

  • export default中的computed属性

【1】单向计算属性computed

  • 案例:输入姓和名,拼接得到姓名(但是姓名反过来显示性和名就报错)

  • person.fullname=computed(function(){
    	return person.fistname+person.lastname
    })
    #由于person.fistname+person.lastname会变,所以函数会自己变
    
    <template>
      <div class="about">
        <h3>姓和名组合</h3>
        <h4>请输入您的姓:<input type="text" v-model="person.fistname"> </h4>
        <h4>请输入您的名:<input type="text" v-model="person.lastname"> </h4>
        <h4>您的全名:<input type="text" v-model="person.fullname"> </h4>
      </div>
    </template>
    
    <script>
    import {ref, reactive, computed} from "vue";
    
    export default {
      name:'AboutView',
      setup(){
        //定义姓名函数
        const person=reactive({fistname:'',lastname:''})
        
        //定义pserson的fullname属性
        person.fullname=computed(function(){
          return person.fistname+person.lastname
        })
        
        return {person}
      },
    }
    </script>
    

【2】双向计算属性set和get

  • 类似变量1=变量2+变量3,当变量2和3变化时候1肯定变,现在1变带着2和3变

  • get是后面变前面在变,set是后面变(根据值分解参数)

  • person.fullName = computed({
    	get() { 
    		return person.firstName + person.lastName},
    	set(value) {
    		person.firstName = value.slice(0, 1)
    		person.lastName = value.slice(1)}
    
    <template>
      <div class="about">
        <h3>姓和名组合</h3>
        <h4>请输入您的姓:<input type="text" v-model="person.fistname"> </h4>
        <h4>请输入您的名:<input type="text" v-model="person.lastname"> </h4>
        <h4>您的全名:<input type="text" v-model="person.fullname"> </h4>
      </div>
    </template>
    
    <script>
    import {ref, reactive, computed} from "vue";
    
    export default {
      name:'AboutView',
      setup(){
        //定义姓名函数
        const person=reactive({fistname:'',lastname:''})
        // const fullname=ref('')
        person.fullname=computed(
        { get(){
          return person.fistname+person.lastname},
          set(value){
             person.fistname=value.slice(0,1)
            person.lastname=value.slice(1)
            return person.fistname,person.lastname}
          })
    
        return {person}
      },}
    </script>
    

【二】监听属性watch

【1】总结watch(value,(newValue, oldValue))

  • export default中的watch方法,放入watch中的值或者变量可以被监听,只要变化就会触发该函数,并且有俩参数(修改前,修改后)

  • watch(height, (newValue, oldValue) => {
        console.log('height变化了', newValue, oldValue)
    })
    
  • watch([变量1, 变量2], (newValue, oldValue) => {
        console.log('1或2变化了', newValue, oldValue)
    })
    

【2】监听值类型

  • <template>
      <div class="about">
        <h3>监听值类型{{height}}</h3>
        <button @click="heightadd">点我增加身高</button>
      </div>
    </template>
    
    <script>
    import {ref, reactive, computed, watch} from "vue";
    
    export default {
      name:'AboutView',
      setup(){
        //监听值类型
        const height=ref(180)
        
        //监听函数
        watch(height,(newvalue,oldvalue)=>{
          console.log(newvalue)
          console.log(oldvalue)})
          
        //变化函数  
        function heightadd(){
          height.value++
        }
        return {count,add,user_info,changename,person,height,heightadd}
      },}
    </script>
    

【2】监听对象类型

  • <h3>监听对象类型{{user}}</h3>
    <button @click="changeuser">点我换人</button>
    
    //监听对象类型
    const user=reactive({username:'zhou',age:18})
    	watch(user,(newvalue,oldvalue)=>{
    	console.log(newvalue)
    	console.log(oldvalue)
    })
    function changeuser(){
    	user.username='zhou6'
    	user.age++
    }
    

【3】多个变量一起监听

  • const sum = ref(100)
    const msg = ref('很好')
    function changeValue(){
        sum.value++
        msg.value='不太好'
    }
    
    watch([sum, msg], (newValue, oldValue) => {
        console.log('sum或msg变化了', newValue, oldValue)
    })
    

【4】watchEffect函数

  • 不需要指定监听谁,只要变量在watchEffect内部使用了,当这个变量发生变化了,就会触发

  • wacth更适用于需要有条件监听数据变化的场景,computed更适合用于创建派生数据和性能优化,wactheffect会追踪函数内部的所有值,消耗比较大

  • 如果多个都同时变化,只会执行一次

    watchEffect(() => {
        const x1 = sum.value
        const x2 = person.name
        console.log('watchEffect配置的回调执行了')
    })
    
  • 14
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Vue3 和 ECharts 实现气泡图的示例代码: 首先,在你的项目安装 echarts 和 vue-echarts: ``` npm install echarts vue-echarts@5 ``` 然后,创建一个 Vue 组件来渲染气泡图: ```vue <template> <div class="bubble-chart" ref="chart"></div> </template> <script> import { defineComponent } from 'vue' import { use } from 'echarts/core' import { CanvasRenderer } from 'echarts/renderers' import { ScatterChart } from 'echarts/charts' import { GridComponent, TooltipComponent } from 'echarts/components' import ECharts from 'vue-echarts' // 使用必要的组件和渲染器 use([CanvasRenderer, ScatterChart, GridComponent, TooltipComponent]) export default defineComponent({ components: { ECharts }, mounted() { this.renderChart() }, methods: { renderChart() { const chart = this.$refs.chart.echartsInstance chart.setOption({ title: { text: '气泡图' }, tooltip: { trigger: 'axis', showDelay: 0, axisPointer: { type: 'cross', lineStyle: { type: 'dashed', width: 1 } } }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, xAxis: [ { type: 'value', scale: true } ], yAxis: [ { type: 'value', scale: true } ], series: [ { name: '气泡图', type: 'scatter', data: [ [10.0, 8.04, 10], [8.0, 6.95, 8], [13.0, 7.58, 13], [9.0, 8.81, 9], [11.0, 8.33, 11], [14.0, 9.96, 14], [6.0, 7.24, 6], [4.0, 4.26, 4], [12.0, 10.84, 12], [7.0, 4.82, 7], [5.0, 5.68, 5] ], symbolSize: function (data) { return Math.sqrt(data[2]) * 5 }, label: { show: true, formatter: function (param) { return param.data[2] }, position: 'top' }, itemStyle: { color: '#4372e6' } } ] }) } } }) </script> <style> .bubble-chart { height: 500px; } </style> ``` 在上面的示例代码,我们使用了 ECharts 的 ScatterChart 组件来实现气泡图。在 series.data 设置了气泡图的数据,其每个点的数据由三个数值组成,分别表示 x 轴、y 轴坐标和气泡大小。通过 symbolSize 属性可以设置气泡的大小,通过 label 属性可以在气泡上显示对应的数值。 最后,在你的页面使用这个组件即可渲染气泡图: ```vue <template> <div> <bubble-chart /> </div> </template> <script> import BubbleChart from './BubbleChart.vue' export default { components: { BubbleChart } } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值