计算属性具有缓存性 相关属性变化时才重新计算,否则返回之前计算的结果
方法 调用总是会在重渲染发生时再次执行函数
将逻辑代码写在计算属性里
<template>
<p>Has published books:</p>
<span>{{ publishedBooksMessage }}</span>
</template>
<script setup>
import { reactive, computed } from 'vue'
const author = reactive({
name: 'John Doe',
books: [
'Vue 2 - Advanced Guide',
'Vue 3 - Basic Guide',
'Vue 4 - The Mystery'
]
})
// 一个计算属性 ref
const publishedBooksMessage = computed(() => {
return author.books.length > 0 ? 'Yes' : 'No'
})
</script>
ref()
import { ref } from 'vue'
const count = ref(0)
reactive()
import { reactive } from 'vue'
const state = reactive({ count: 0 })
Map、Set
这样的集合类型)。它不能持有如 string、number 或 boolean
这样的原始类型。import { createApp } from 'vue'
const app = createApp({
/* 根组件选项 */
})
npm create vue@latest
? Project name: … <your-project-name>
? Add TypeScript? … No / Yes
? Add JSX Support? … No / Yes
? Add Vue Router for Single Page Application development? … No / Yes
? Add Pinia for state management? … No / Yes
? Add Vitest for Unit testing? … No / Yes
? Add an End-to-End Testing Solution? … No / Cypress / Nightwatch / Playwright
? Add ESLint for code quality? … No / Yes
? Add Prettier for code formatting? … No / Yes
? Add Vue DevTools 7 extension for debugging? (experimental) … No / Yes
Scaffolding project in ./<your-project-name>...
Done.
cd <your-project-name>
npm install
npm run dev