vue3学习之ref的妙用

我爱海鲸 2023-01-10 14:47:51 前端

简介ref的妙用

链接上一篇文章:vue3学习之快速搭建

1、ref的使用,如图:

App.vue:

<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <h1>{{count}}</h1>
    <h1>{{double}}</h1>
    <button @click="increase">赞+1</button><br/>
  </div>
</template>

<script lang="ts">
  // 响应式对象引入,计算函数引入
import { ref,computed } from 'vue'

export default{
  name: 'App',
  setup() {
    const count = ref(0);
    const double = computed(()=> {
      return count.value *2;
    })
    const increase = () => {
      count.value++
    }
    return {
      count,
      double,
      increase
    }
  }
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

显示的效果

在浏览器中打开:http://localhost:8080/

这样我们的第一个vue3的使用案例就完成了。

你好:我的2025