Vue 3 学习 setup与响应式

 setup

  如以下截图所示:

代码如下:


<script setup>
const info = '简单的信息'

const printInfo = () => {
  console.log(info)
}

</script>


<template>
  <div class="about">
    <h1>{{ info }}</h1>
    <button @click="printInfo">Button</button>
  </div>
</template>

<style>
@media (min-width: 1024px) {
  .about {
    min-height: 100vh;
    display: flex;
    align-items: center;
  }
}
</style>

如果了解过或者使用过Vue 2的同学就会知道,以上代码多么简洁。这就是Vue3的好处。

我是后悔没有早点学习,省了一大堆罗嗦的代码,列如data, methods等等。

响应式

如下截图:

代码如下:


<script setup>

import { ref } from 'vue'

const info = '简单的信息'

const printInfo = () => {
  console.log(info)
}

const count = ref(0)

const setCount = () => {
  count.value++
}

</script>


<template>
  <div class="about">
    <div>
      <h1>{{ info }}</h1>
      <button @click="printInfo">Button</button>
    </div>
    <div>
      <span> {{ count }} </span>
      <button @click="setCount"> +1 </button>
    </div>
  </div>
</template>

<style>
</style>

结束,为什么这么写和原理是什么,不作说明。只是个人的学习记录。详细的教程还请不小心刷到这篇博客的同学们,去B站搜索Vue 3,有无数Up,讲的都很好的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值