当前位置:网站首页>Composition API 前提
Composition API 前提
2022-07-07 03:12:00 【要成为光的女人】
一、setup的用途
1.为了开始使用Coposition API,我们需要有一个可以实际使用它(编写代码的地方);
2.在Vue组件中,这个位置就是setup函数;
二、setup的认识理解
setup其实就是组件的另外一个选项:
只不过这个选项强大到我们可以用它来代替之前所缩写的大部分其他选项;
比如methods、computed、watch、data、生命周期等等。
三、setup 函数的使用(注意在setup里面是没有绑定this的)
- 首先我们得明白setup函数又哪些参数?
A:它主要有两个参数:
第一个参数:props (父组件传递过来的属性)
第二个参数:context:它里面包含了三个属性
attrs:所有的非props的attribute;
slots:父组件传递过来的插槽(这个在以渲染函数返回时会有作用)
emit:当我们组件内部需要发出事件时会用到emit(因为我们不能访问this,所以不可以通过this.$emit发出事件)
export default {
props:{
message:{
type:String,
required:true
}
},
// 参数1:props,父组件传递过来的属性
// 参数2:
// 方式1:
// setup(props,context){
// console.log(props.message); //参数1
// console.log(context.attrs.id)
// }
//
// 方式2:对context做一个解构
setup(props,{attrs,slots,emit}){
console.log(props.message); //参数1
console.log(attrs.id);
console.log(slots);
console.log(emit);
}2.setup函数有什么样的返回值
setup返回值可以在模版template中被使用;
也就是说我们可以通过setup的返回值来代替data选项;
setup(){
let counter=100;
//1.在setup 里面在定义一个局部函数
const increat=()=>{
counter++
console.log(counter);
}
return{
counter,
increat //2.在将这个函数返回出去
}
}
四、setup 不可以使用this
- 表达的含义是this并没有指向当前组件实例;
- 并且在setup被调用之前,data、computed、methods等都没有被解析
- 所以无法在setup中获取this;
五、如何阅读源码:

六、如何在setup里面让数据变成响应式的
(1)reactive API 帮助数据实现响应式
<template>
<div id="app">
<!-- 数据展示 -->
<span>当前计数:{
{state.counter}}</span>
<button @click="increat"></button>
</div>
</template>
<script>
// 1.vue3 提供的reactive API 做响应式的
import {reactive} from 'vue'
export default {
props:{
message:{
type:String,
required:true
}
},
setup(){
//2.现在把响应式的数据用reactive 做一个包裹
const state=reactive({
counter:100;
})
//3.在setup 里面在定义一个局部函数
const increat = ()=>{
state.counter++;
console.log(state.counter);
}
// 返回的数据
return {
state,
increat //2.在将这个函数返回出去
}
}
}
</script>(2)ref API
reative API 对传入的类型是有限制的,它要求我们传入必须是一个对象或者数组类型;
如果我们传入一个基本数据类型(String、Number、Boolean)会报一个警告;
这个时候Vue3给我们提供了另一个API:ref API
- ref 会返回一个可变的响应式对象,该对象作为一个响应式引用维护它内部的值,这就是ref名称的来源
- 它内部的值是在ref的value属性中被维护的;
这里有两个注意事项:
在模版引入ref的值时, Vue会自动帮助我们进行解包操作,所以我们并不需要在模版中通过ref.value的方式来使用
<template> <div id="app"> <!-- 当我们在template 模版中使用ref对象,他会自动进行解包的操作, --> <span>当前计数:{ {counter}}</span> <button @click="increat"></button> </div> </template> <script> // 1.vue3 提供的reactive API 做响应式的 import {ref} from 'vue' export default { props:{ message:{ type:String, required:true } }, setup(){ //counter 变成一个ref的可响应式的引用 let counter=ref(100); //3.在setup 里面在定义一个局部函数 const increat = ()=>{ counter.value++; console.log(counter); } // 返回的数据 return { counter, increat //2.在将这个函数返回出去 } } } </script>
边栏推荐
- 多线程与高并发(9)——AQS其他同步组件(Semaphore、ReentrantReadWriteLock、Exchanger)
- 偏执的非合格公司
- 请教一下,监听pgsql ,怎样可以监听多个schema和table
- Installing redis and windows extension method under win system
- JWT的基础介绍
- linux系统rpm方式安装的mysql启动失败
- 请问 flinksql对接cdc时 如何实现计算某个字段update前后的差异 ?
- 2022/07/04学习记录
- . Net core accesses uncommon static file types (MIME types)
- 请教一个问题,flink oracle cdc,读取一个没有更新操作的表,隔十几秒就重复读取全量数据
猜你喜欢

MATLAB小技巧(30)非线性拟合 lsqcurefit

Maze games based on JS

String (explanation)

带你刷(牛客网)C语言百题(第一天)

How can brand e-commerce grow against the trend? See the future here!

After the promotion, sales volume and flow are both. Is it really easy to relax?
![Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]](/img/13/096857158c9f977f8677f7cd0f9d4b.png)
Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]

如何给目标机器人建模并仿真【数学/控制意义】

Anr principle and Practice

Take you to brush (niuke.com) C language hundred questions (the first day)
随机推荐
学术报告系列(六) - Autonomous Driving on the journey to full autonomy
JWT的基础介绍
服装门店如何盈利?
2022年全国所有A级景区数据(13604条)
Comment les entreprises gèrent - elles les données? Partager les leçons tirées des quatre aspects de la gouvernance des données
Answer to the second stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
Brand · consultation standardization
Please tell me how to monitor multiple schemas and tables by listening to PgSQL
RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling `cublasCreate(handle)`问题解决
分布式id解决方案
jdbc数据库连接池使用问题
After the promotion, sales volume and flow are both. Is it really easy to relax?
Stack and queue-p78-8 [2011 unified examination true question]
Abnova循环肿瘤DNA丨全血分离,基因组DNA萃取分析
Multidisciplinary integration
联合索引ABC的几种索引利用情况
数据资产管理与数据安全国内外最新趋势
Unable to debug screen program with serial port
The startup of MySQL installed in RPM mode of Linux system failed
Cloudcompare point pair selection