学习-vue3 OPtions API 和 Composition API

Options API:

<template>
  <div class="home">
    vue3知识点练习......
    <div>
      <button @click="increment">count 是: {{ count }}</button>
    </div>

  </div>
</template>

<script>
// @ is an alias to /src

export default {
  name: 'Home',
  data () {
    return {
      // 响应式数据 可通过vue实例this访问到
      count: 0
    }
  },
  // 组件挂载 该生命周期钩子被调用
  mounted () {
    console.log('count的初始值是:' + this.count)
  },
  methods: {
    increment () {
      this.count++
    }
  }
}
</script>

<style lang="scss" scoped>
  button {
    font-weight: bold;
  }
</style>

Composition API:

<template>
  <div class="home">
    vue3知识点练习......
    <div>
      <button @click="increment">count 是: {{ count }}</button>
    </div>

  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'

// 响应式数据 初始化
const count = ref(0)
function increment () {
  count.value++
}
// 生命周期钩子
onMounted(() => {
  console.log('初始值count:' + count.value)
})
</script>

<style lang="scss" scoped>
  button {
    font-weight: bold;
  }
</style>

效果展示:

posted on 2022-06-23 09:04  法老的微笑  阅读(188)  评论(0)    收藏  举报