大飞_dafei

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

Vue 中计算属性(computed)和侦听器(watch)

Vue 中计算属性(computed)和侦听器(watch)

复制代码
<template>
    <div>
        <p>原始字符串: {{ message }}</p>
        <p>计算后反转字符串: {{ reversedMessage }}</p>
        
        
        <input type="text" v-model="foo" style="width: 50px"> +
        <input type="text" v-model="bar" style="width: 50px">
        <span>computed__求和__ <font color="#ff6b81">{{all}}</font></span>
        <ul v-for="(item) in allArr">
            <li v-if="item.id==2">{{item.name}} <font color="#ff6b81">{{item.price}}</font></li>
            <li v-else>{{item.name}} <font>{{item.price}}</font></li>
        </ul>
        
        
        <br>
        <br>
        <br>
        <input type="text" v-model="allCount">
        <font color="#ff6b81">watch__{{allCountWatch}}</font>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                message: 'Hello wolrd!',
                foo: 1,
                bar: 3,
                feiFoo: [
                    {id: 1, name: "史记 ___ ", price: 100},
                    {id: 2, name: "汉书 ___ ", price: 200},
                ],
                allCount: "开始监听",
                allCountWatch: "监后开始做事情",
            }
        },
        computed: {
            reversedMessage () {
                return this.message.split('').reverse().join('')
            },
            all () {
                this.feiFoo[1].price = Math.floor(Math.random()*10000);
                return Number(this.foo) + Number(this.bar);
            },
            "allArr":function (a,b) {
                return this.feiFoo;
            }
        },
        watch:{
            allCount(val) { // 监听到后做其他事情
                console.log(val);
                this.allCountWatch = `已经监听到__${val}`;
            }
        }
    };
</script>

<style scoped>
 
</style>
复制代码

计算属性和侦听器

 

 

vue3中

只读属性

复制代码
<template>
  <h3>计算属性(computed) 只有getter的计算属性</h3>
  <p>
    fistName: <input v-model="user.firstName"/><br>
    lastName: <input v-model="user.lastName"/><br>
    fullName1: <input v-model="fullName1" disabled/>  只是获取 <br>
  </p>
</template>

<script setup>
import {reactive, computed} from "vue";

const user = reactive({
  firstName: '',
  lastName: '仲尼'
})

// 只有getter的计算属性
const fullName1 = computed(() => {
  console.log('fullName1')
  return user.firstName + '-' + user.lastName
})

</script>
复制代码

读和写2个属性

复制代码
<template>
  <h3>计算属性(computed) 有getter与setter的计算属性</h3>
  <p>
    fistName: <input v-model="user.firstName"/><br>
    lastName: <input v-model="user.lastName"/><br>
    fullName2: <input v-model="fullName2"> 获取到,自己修改其他地方也改<br>
  </p>
</template>

<script setup>
import {reactive, computed} from "vue";

const user = reactive({
  firstName: '',
  lastName: '仲尼'
})

// 有getter与setter的计算属性
const fullName2 = computed({
  get () {
    console.log('fullName2 get')
    return user.firstName + '-' + user.lastName+'-'+'把我返回给set方法'
  },

  set (value) { // 获取到get方法的返回值,
    console.log('fullName2 set',value)
    const names = value.split('-')
    user.firstName = names[0]
    user.lastName = names[1]
  }
})

</script>
View Code
复制代码

 

posted on   大飞_dafei  阅读(98)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示