执行setup时,只能访问一下property:
props
attes
slots
emit
无法访问到
data
computed
methods
refs
单文件组件(SFC)
包含了html、js、css语言。
setip语法糖
<script setup>
// 定义响应式变量,还是需要从vue中引入
import {ref} from 'vue'
// vue3 引入组件不需要在methods注册
import Qwer from './components/Qwer.vue'
// 定义变量,在模板使用不需要暴露出去,模板直接使用
const a=20
console.log(a);
const b=ref(1)
function adds(){
b.value++
}
</script>
<template>
<div>
<h2>{{ b }}</h2>
<button @click="adds">改变++</button>
<h2>{{ a }}</h2>
<Qwer></Qwer>
</div>
</template>
<style scoped>
</style>
顶层的绑定会被暴露给模板
(包含变量,函数声明,以及import引入内容)都能在模板中直接使用。