setup()

基本使用

<template>
    <el-button type="primary" @click="handle">{{ count }}</el-button>
    <el-button type="primary" @click="handle1">统计</el-button>
</template>

<script>
export default {
    setup() {
        // 定义变量  const不能改 let 跟var一样
        let count = 1;
        // 定义函数
        const handle = () => {
            // 数据变了,页面没变
            count++
        }
        const handle1 = () => {
            console.log(count);
        }
        // 要在模版中使用必须,return出去
        return {
            count, handle, handle1
        }
    }
}
</script>

响应式

<template>
    <span> {{person.age}}</span>
    <el-button type="primary" @click="handle">{{ count }}</el-button>
</template>

<script>
import { reactive, ref } from 'vue';

export default {
    setup() {
        // 动态数据
        let count = ref(0)
        let person =reactive({
            name:'szw',
            age:12
        })
        // 定义函数
        const handle = () => {
            // 数据变了,页面也变
            count.value++
            person.age++
        }
        // 要在模版中使用必须,return出去
        return {
            count, handle,person
        }
    }
}
</script>

本文作者:Sherwin

本文链接:https://www.cnblogs.com/sherwin1995/p/16871110.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Sherwin_szw  阅读(30)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.