setup的组合特点:1. 更加直观,接近原生js,2. 按需加载,3. 没有this,降低了耦合性,提高复用性。setup()相当于created生命周期,需要 return 返回,在模板中使用。
setup中常出现的事件有:
ref值类型 响应对象、reactive引用类型 响应式、watch监听、watchEffect监听效果、computed
计算、生命周期、全局配置、getCurrentInstance获取当前实例、setup使用vuex、setup 使用路由、setup(props,context)参数。
ref:const count = ref(5),在setup访问count值,count.value 来访问。可以作为dom引用:
const inp = ref(null)
<p ref="inp">
inp.value.innerText。
reactive: const list = reactive(默认值)
list = xxx 直接赋值破坏了引用
list.push(xxx)
watch: watch("count",(nval,oval)=>{
//执行回调函数
})
watchEffect: const stop = watchEffect(()=>{
localStorage.setItem("count",count.value)
});stop用来停止监听,stop()。