Vue3_响应式数据和setup语法糖

目录

使用vite创建一个vue + JavaScript项目

clike 复制代码
npm create vite

进入项目目录下,然后执行如下命令

clike 复制代码
npm i

启动项目
npm run dev

删除不必要的基础结构

main.js中删除样式

删除App.vue中不必要内容

只保留如下内容

添加代码功能

clike 复制代码
<script >
export default{
  setup(){
    // 定义一些要展示到html上的一些数据   变量/对象
    let counter = 100
    function counterIncr(){
        counter++
    }
    function counterDesc(){
        counter--
    }
    function showCounter(){
      alert(counter)
    }

    return{
      counter,
      counterIncr,
      counterDesc,
      showCounter
    }
    
  }
}
</script>

<template>
<div>
  <button @click="counterIncr">+</button>
  <span v-text="counter"></span>
  <button @click="counterDesc">-</button>
  <button @click="showCounter">显示值</button>

</div>
 
</template>

<style scoped>

</style>

v-text表示将某个变量的值作为双标签中的文本来展示

此时只有窗口上显示值变化,页面上的不变,可通过如下方式解决

响应式数据

在数据变化时,vue框架会将变量最新的值更新到dom树中,页面数据就是实时最新的

非响应式数据

在数据变化时,vue框架不会将变量最新的值更新到dom树中,页面数据就不是实时最新的

在vue2中,数据不做特殊处理,默认就是响应式的

vue3中,数据要经过ref / reactive函数的处理才是响应式的

ref reactive函数是vue框架给我们提供的方法,导入进来即可使用

clike 复制代码
import {ref} from 'vue'

在script标签中,操作ref的响应式数据需要通过.value的形式操作

在template标签中,操作ref的响应式数据,无需使用.value

clike 复制代码
<script >
import {ref} from 'vue'
export default{
  setup(){
    // 定义一些要展示到html上的一些数据   变量/对象
    let counter = ref(100)
    function counterIncr(){
        counter.value++
    }
    function counterDesc(){
        counter.value--
    }
    function showCounter(){
      alert(counter.value)
    }

    return{
      counter,
      counterIncr,
      counterDesc,
      showCounter
    }
    
  }
}
</script>

<template>
<div>
  <button @click="counterIncr">+</button>
  <span v-text="counter"></span>
  <button @click="counterDesc">-</button>
  <button @click="showCounter">显示值</button>

</div>
 
</template>

<style scoped>

</style>

问题解决

setup语法糖

可以通过setup语法糖简化该代码

clike 复制代码
<script setup >
import {ref} from 'vue'

    // 定义一些要展示到html上的一些数据   变量/对象
    let counter = ref(100)
    function counterIncr(){
        counter.value++
    }
    function counterDesc(){
        counter.value--
    }
    function showCounter(){
      alert(counter.value)
    }


</script>

<template>
<div>
  <button @click="counterIncr">+</button>
  <span v-text="counter"></span>
  <button @click="counterDesc">-</button>
  <button @click="showCounter">显示值</button>

</div>
 
</template>

<style scoped>

</style>
相关推荐
三十而立洋4 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
卡布鲁4 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
李少兄4 天前
JavaScript 隐式全局变量解析
javascript
沙蒿同学5 天前
我用 Go 搭了一条 AI Agent 流水线:从 1 张商品图到一整套淘宝详情页
前端·javascript·后端
paopaokaka_luck5 天前
智慧社区综合服务小程序(人脸识别、AI问答、Echarts图形化分析)
前端·javascript·spring boot·spring·数据分析·echarts
Hilaku5 天前
为什么死磕 1px 的团队,用户体验反而更差?
前端·javascript·程序员
陈拉斯5 天前
给公司年会写了个大屏抽奖系统:动画在前端跑,凭什么说结果没被改?
javascript
yzy855 天前
Vue中下载或打开文件的JavaScript方法
前端·javascript·vue
用户298698530145 天前
在 React 中用 JavaScript 将 Word 转换为文本
前端·javascript·react.js
李少兄5 天前
深入解析 JavaScript 中的 `document` 对象
javascript