vue3 Study(1)

一、初识vue3

https://juejin.cn/post/7487815126301818880

二、VUE3基础之组合式开发和setup

https://juejin.cn/post/7488176279921934376

三、VUE3基础之ref和reactive创建响应式数据

https://blog.csdn.net/qq_58195504/article/details/145160950?spm=1001.2014.3001.5502

四、解构赋值,toRefs和toRef把响应式对象里面的数据结构出来依然是响应式数据

四、VUE3基础之计算属性

https://juejin.cn/post/7491485553030316082

复制代码
<script setup>
    import { computed,ref } from 'vue';
    let firstName = ref('zhang')
    let lastName = ref('san')
    // 下面这种方式只能读取
    // let fullName = computed(()=>{
    //     return firstName.value+'-' +lastName.value
    // })

    // 下面这种方式可以编辑,也可以读取
    let fullName = computed({
        get(){
            return firstName.value+'-' +lastName.value
        },
        set(val){
            let [str1,str2] = val.split('-')
            firstName.value = str1
            lastName.value = str2
        }
    })
    function changeName(){
        fullName.value = 'li-si'
    }
    

</script>
<template>
   姓:<input type="text" v-model="firstName"></input>
   名:<input type="text" v-model="lastName"></input>
   姓名:<span>{{ fullName }}</span>
    <button @click="changeName">改名</button>
</template>

五、监听器

https://juejin.cn/post/7498258715231092762

情况一、监视ref定义的数据,直接写数据名就可以(即:不需要添加.value)

复制代码
<script setup>
import {ref ,reactive,watch} from 'vue'
let count =  ref(0)
function add(){
    count.value +=1
}
watch(count,()=>{
    console.log('count改变了11111111')
})

</script>
<template>
<div>{{ count }}</div>
<button @click="add">点击+1</button>
</template>

情况二、监视ref定义的·对象类型的数据,若要监视内部数据的变动,需要手动开启深度监视

复制代码
<script setup>
import {ref,watch} from 'vue'
let car =  ref({brand:'玛莎拉蒂',price:100})
function changeBrand(){
    car.value.brand = '奥迪'
}
function changePrice(){
    car.value.price = 60
}
function changeCar(){
    car.value =  {brand:'比亚迪',price:9}
}
watch(car,()=>{
    console.log('car改变了11111111')
},{deep:true})

</script>
<template>
<div>{{ car.brand }}的价钱是{{ car.price }}万</div>
<button @click="changeBrand">更改品牌</button>
<button @click="changePrice">更改价格</button>
<button @click="changeCar">更改整个车</button>
</template>

情况三、使用reactive监视对象数据的属性,默认是开启深度监视的,即不需要添加deep:true,就可以直接监视内部数据的改变

复制代码
<script setup>
import {reactive,watch} from 'vue'
let car =  reactive({brand:'玛莎拉蒂',price:100})
function changeBrand(){
    car.brand = '奥迪'
}
function changePrice(){
    car.price = 60
}
function changeCar(){
    Object.assign(car,{brand:'aima',price:2})
}
watch(car,()=>{
    console.log('car改变了11111111')
},{deep:true})

</script>
<template>
<div>{{ car.brand }}的价钱是{{ car.price }}万</div>
<button @click="changeBrand">更改品牌</button>
<button @click="changePrice">更改价格</button>
<button @click="changeCar">更改整个车</button>
</template>

情况四、监视多个属性

复制代码
<script setup>
import {reactive,watch} from 'vue'
let person =  reactive({
    name:'李现',
    car:{
        brand:'玛莎拉蒂',
        price:100
    }
})
function changeName(){
    person.name = '杨紫'
}
function changeBrand(){
    person.car.brand = '奥迪'
}
function changePrice(){
    person.car.price = 60
}
function changeCar(){
    Object.assign(person.car,{brand:'aima',price:2})
}
watch([()=>person.name,person.car],()=>{
    console.log('姓名改变了11111111')
},{deep:true})

</script>
<template>
<div>{{person.name}}购买{{ person.car.brand }}的价钱是{{ person.car.price }}万</div>
<button @click="changeName">更改名字</button>
<button @click="changeBrand">更改品牌</button>
<button @click="changePrice">更改价格</button>
<button @click="changeCar">更改整个车</button>
</template>

六、watch和watchEffect

(1) 都能监听响应式数据的变化,不同的是监听数据变化的方式不同。

(2) **watch:**要明确指出监视的数据。

(3) **watchEffect:**不用明确指出监视的数据(函数用到哪些属性,就自动监视哪些属性)

复制代码
<template>
    <div>水温:{{temp}}℃</div>
    <div>高度:{{ height}}m</div>
    <button @click="changeTemp">改变水温</button>
    <button @click="changeHeight">改变高度</button>
</template>
<script setup>
    import {ref,watch,watchEffect} from 'vue'
    let temp = ref(0)
    let height = ref(0)
    function changeTemp(){
        temp.value+=10
    }
    function changeHeight(){
        height.value+=20
    }
    // watch([temp,height],(value)=>{
    //     let [newTemp,newHeight] = value
    //     if(newTemp>=50 || newHeight>=40){
    //         console.log('向服务器发请求');
            
    //     }
    // })

    watchEffect(()=>{
        if(temp.value>=50 || height.value>=40){
            console.log('向服务器发请求');
            
        }
    })
</script>

七、VUE3基础之标签的ref属性

https://juejin.cn/post/7501880110838284340

八、VUE3基础之props

https://juejin.cn/user/2496335488221770/posts

九、生命周期

https://juejin.cn/post/7508591120407052298

十、组件通信

https://blog.csdn.net/qq_58195504/article/details/145608245?spm=1001.2014.3001.5502

相关推荐
wuhen_n1 小时前
网络请求在Vite层的代理与Mock:告别跨域和后端依赖
前端·javascript·vue.js
小彭努力中1 小时前
193.Vue3 + OpenLayers 实战:圆孔相机模型推算卫星拍摄区域
vue.js·数码相机·vue·openlayers·geojson
用户69371750013846 小时前
Google 正在“收紧侧加载”:陌生 APK 安装或需等待 24 小时
android·前端
蓝帆傲亦6 小时前
Web 前端搜索文字高亮实现方法汇总
前端
用户69371750013846 小时前
Room 3.0:这次不是升级,是重来
android·前端·google
漫随流水8 小时前
旅游推荐系统(view.py)
前端·数据库·python·旅游
踩着两条虫9 小时前
VTJ.PRO 核心架构全公开!从设计稿到代码,揭秘AI智能体如何“听懂人话”
前端·vue.js·ai编程
jzlhll12310 小时前
kotlin Flow first() last()总结
开发语言·前端·kotlin
用头发抵命10 小时前
Vue 3 中优雅地集成 Video.js 播放器:从组件封装到功能定制
开发语言·javascript·ecmascript
蓝冰凌11 小时前
Vue 3 中 defineExpose 的行为【defineExpose暴露ref变量】详解:自动解包、响应性与实际使用
前端·javascript·vue.js