• vue3-setter/getter和计算属性


    <template>
      <div id="app">
        <p>姓名:{{name}}</p>
        <p>
          <button @click="changeAge(-1)">-</button>
          年龄:{{age}}
          <button @click="changeAge(1)">+</button>
        </p>
        <p>出生年份:<button @click="changeYear(-1)">-</button>
          {{year}}
          <button @click="changeYear(1)">+</button>
        </p>
      </div>
    </template>
    
    <script>
    import {ref,computed} from 'vue'
    export default {
      name:'app',
      data(){
        return {
          name:'xiaosan'
        }
      },
      setup(){
        const name =ref('小四')
        const age=ref(18)
        const year=computed({
          get:()=>{
            return 2020-age.value
          },
          set: val=>{
            age.value=2020-val;
          }
        });
        // const year=computed(()=>{
        //   return 2020-age.value
        // })
        function changeAge(val){
          age.value +=val //想改变值或获取值 必须.value
        }
        function changeYear(val){
          year.value=year.value+val
        }
        return { //必须返回 模板中才能使用
          name,age,changeAge,year,changeYear
        }
      }
    }
    </script>
  • 相关阅读:
    Java实现二叉排序树
    servlet/filter/listener/interceptor区别与联系
    Java中创建对象的5种方式
    字符串练习
    成员变量、类变量、局部变量的区别
    强制清除gradle 缓存
    XML
    jQuery
    JavaScript
    CSS
  • 原文地址:https://www.cnblogs.com/lxz-blogs/p/13595019.html
Copyright © 2020-2023  润新知