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

相关推荐
kyriewen20 小时前
我用AI把公司10万行代码屎山重构了,CTO看了代码后说:你提前转正
前端·javascript·ai编程
ttwuai20 小时前
XYGo Admin 菜单与路由:Vue3 动态路由 + GoFrame 权限菜单的完整实现方案
前端·vue·后台框架
程序员码歌20 小时前
OpenSpec 到 Superpowers:AI 编码从说清到做对
android·前端·人工智能
爱编程的小新☆20 小时前
LangGraph4j工作流框架
前端·数据库·ai·langchain·langgraph4j
北风toto20 小时前
为什么 IntelliJ IDEA Community 无法开发 Vue?——附解决方案
java·vue.js·intellij-idea
@PHARAOH20 小时前
HOW - 构建一个轻量前后端一体服务
前端·微服务·服务端
无限进步_20 小时前
【C++】C++11的类功能增强与STL变化
java·前端·数据结构·c++·后端·算法
一只小小Java20 小时前
Echarts单表多图实现
前端·javascript·echarts
跟着珅聪学java20 小时前
Element UI 的 Tabs 标签页开发教程
javascript·vue.js·elementui
dunky20 小时前
Spring AI 深度解析:把 LLM 抽象成 Spring Bean 的底层逻辑
前端