响应式数据是vue的一大亮点,下面我们来聊聊如何声明响应式数据
目录
直接声明变量
- 直接声明变量并不能使之拥有响应性
- 示例:
<script setup>
let num = 0;
const clickFun = () => {
console.log("num:", ++num);
}
</script>
<template>
<div style="display:flex;flex-direction: column;">
<span>num : {{num}}</span>
<button @click="clickFun()">修改</button>
</div>
</template>
- 运行结果:
![](https://blog.coolight.cool/wp-content/uploads/2022/07/image-10.png)
![](https://blog.coolight.cool/wp-content/uploads/2022/07/image-11.png)
- 多次点击按钮后,控制台打印的num值虽然有增加,但在html页面显示的num值却不变
ref(基本数据类型)
- 使用ref(基本数据类型)即可把一个基本数据类型的值转化为响应式并返回
- 基本数据类型:
- String
- Number
- BigInt
- Boolean
- Symbol
- Undefined
- Null
- 示例:
<script setup>
import { ref } from 'vue';
let num = 0;
let refNum = ref(0);
console.log("refNum:", refNum);
console.log("refNum.value", refNum.value);
const clickFun = () => {
console.log("num:", ++num);
console.log("refNum.value:" , ++refNum.value);
}
</script>
<template>
<div style="display:flex;flex-direction: column;">
<span>num : {{num}}</span>
<span>refNum : {{refNum}}</span>
<button @click="clickFun()">修改</button>
</div>
</template>
- 运行结果:
![](https://blog.coolight.cool/wp-content/uploads/2022/07/image-12.png)
![](https://blog.coolight.cool/wp-content/uploads/2022/07/image-13.png)
- 注意ref()返回的是一个refImpl的对象,它的value属性才是我们要的数据,所以在js中操作时需要.value,但在<template>中会自动展开为内部值
- 可以看到,通过ref()获取的变量是响应式的,会随着变量值的改变而体现在html页面中
reactive(对象)
- reactive()需要我们传入一个对象
- 示例:
<script setup>
import { ref,reactive } from 'vue';
let num = 0;
let refNum = ref(0);
let rea = reactive({
num:0
});
console.log("refNum:", refNum);
console.log("refNum.value", refNum.value);
console.log("rea", rea);
const clickFun = () => {
console.log("num:", ++num);
console.log("refNum.value:" , ++refNum.value);
console.log("rea.num:", ++rea.num);
}
</script>
<template>
<div style="display:flex;flex-direction: column;">
<span>num : {{num}}</span>
<span>refNum : {{refNum}}</span>
<span>rea.num : {{rea.num}}</span>
<button @click="clickFun()">修改</button>
</div>
</template>
- 运行结果:
![](https://blog.coolight.cool/wp-content/uploads/2022/07/image-15.png)
![](https://blog.coolight.cool/wp-content/uploads/2022/07/image-17.png)
- reactive()也可以让变量得到响应性
- 但它需要传入对象,而ref()需要传入基本数据类型
- 而且它返回的是一个proxy对象,但我们在js操作和在<template>绑定时,都可以直接访问和操作其内部变量。
dark web websites deep web drug store
dark web access dark web markets
deep web links tor markets links
darknet market list blackweb
darknet drug store dark internet
darknet links tor market
best price for synthroid 50 mcg
strattera cost
tor markets 2023 tor darknet
darknet drugs tor markets
where can i get viagra
darknet markets 2023 dark market link
dark market list deep dark web
lyrica pills 50 mg
generic lyrica usa
darknet search engine tor market links
generic cialis soft
tor market url deep web sites
tor markets dark market 2023
darkmarket list darknet drug market
darknet markets 2023 deep dark web
deep web drug store tor dark web
deep web search dark market 2023
tor marketplace darkmarket list
deep web links dark net
darknet market tor dark web
darknet drugs dark web link
tor markets 2023 dark market 2023
dark web market list dark markets
deep web search dark web sites
darkmarket link deep web drug store
blackweb tor markets
gabapentin tablets for sale
black internet darkmarket list
baclofen 60 mg
dark web access dark website
drug amoxicillin 500mg
darknet market lists drug markets onion
darknet market list dark web search engines
tor market url how to access dark web
retino 0.25
darknet marketplace dark market url
dark web link deep dark web
deep web drug store blackweb
darkmarket link dark market
diflucan buy in usa
dark markets 2023 dark web site
dark internet dark web market links
dark internet darkmarket list
darknet search engine darknet drug market