vue学习笔记

结合目录,点击阅读

文章目录

  • 案例1:第一行vue代码
    • App.vue引入Person.vue
    • 案例:改变变量的值
    • 案例:改变对象属性值
    • 案例:toRefs进行解包
    • 案例:给名字首字母大写
    • 案例:监视变量值的变化
    • 案例:监视对象属性值的变化
    • 案例:监视reactive对象属性值的变化
    • 案例:监视reactive对象定义的某个属性值
    • 案例:监视reactive对象定义的多个属性值
    • 案例:watchEffect监视ref变量的值
    • 案例:defineExpose通信
    • 案例:组件通信
    • 案例:状态与钩子函数
    • 案例:Component声明与使用
    • 案例:创建路由器
    • 案例:创建子路由
    • 案例:路由传参
    • 案例:路由props
    • 案例:路由重定向
    • 案例:store
    • 案例:数据修改
    • 案例:pina中storeToRefs
    • 案例:pinia进行数据改变
    • 案例:defineProps
    • 案例:shallowRef
    • 案例:readOnly与shallowReadOnly
    • 案例:markRaw与toRaw
    • 案例:customRef
    • 案例:telePort
    • 案例:fallback
    • app相关的操作
    • 案例:作用域插槽
    • 案例:组件通信1
    • 案例:组件通信2-customEvent
    • 案例:组件通信3-mitt
    • 案例:组件通信4
    • 案例:组件通信5
    • 案例:组件通信6
    • 案例:组件通信7
    • 案例:组件通信8

案例1:第一行vue代码

在这里插入图片描述

App.vue引入Person.vue

在这里插入图片描述

<template><div class="app"><h1>你好啊!</h1><Person/></div>
</template><script lang="ts">
import Person from './Person.vue'export default {name: 'App',components: {Person}
}
</script>

Person.vue

<template><div class="person"><h2>姓名:{{a}}</h2></div></template><script lang="ts" setup name="Person5467">let a = 666
</script><style scoped>.person {background-color: skyblue;box-shadow: 0 0 10px;border-radius: 10px;padding: 20px;}
</style>

案例:改变变量的值

 const age = ref(18)age.value = 21

案例:改变对象属性值

html遍历游戏列表

<ul><li v-for="g in games" :key="g.id">{{g.name}}</li>
</ul>
let car = reactive({brand:'奔驰',price:100})let games = reactive([{id:'001',name:'王者荣耀'},{id:'002',name:'原神'},{id:'003',name:'三国志'}])
function changePrice() {car.price += 10console.log(car.price)
}
function changeFirstGame() {games[0].name = "三国志123"
}

案例:toRefs进行解包

let person = reactive({name:'张三',age:18})//toRef与toRefs都是将响应式对象进行解包let {name,age} = toRefs(person)

案例:给名字首字母大写

 全名: <span>{{ fullname }}</span><br> <button @click="changeFullName">将全名改</button>

ts代码

let fullname = computed({get() {return firstname.value.slice(0,1).toUpperCase() + firstname.value.slice(1) + "-" + lastname.value},set(val) {const[str1,str2] = val.split('-')firstname.value = str1lastname.value = str2}})

案例:监视变量值的变化

let sum = ref(0)
function changeSum() {sum.value += 1 
}watch(sum,(newValue,oldValue)=>{console.log('sum变化了',newValue,oldValue)
})

案例:监视对象属性值的变化

let person = ref({name:'张三',age:18})
watch(person,(newValue,oldValue)=>{console.log('person变化了',newValue,oldValue)
},{deep:true})

案例:监视reactive对象属性值的变化

let person = reactive({name:'张三',age:18
})
let obj = reactive({a :{b:{c:555}}
})
watch(person,(newValue,oldValue)=>{console.log('person变化了',newValue,oldValue)
})
watch(obj,(newValue,oldValue)=>{console.log('Obj变化了',newValue,oldValue)
})

案例:监视reactive对象定义的某个属性值

let person = reactive({name:'张三',age:31,model:'汽车',car:{c1:'奔驰',c2:'宝马'}
})
watch(()=>person.name,(newValue,oldValue)=>{console.log(newValue,oldValue)
})

案例:监视reactive对象定义的多个属性值

let person = reactive({name:'张三',age:31,model:'汽车',car:{c1:'奔驰',c2:'宝马'}
})
watch(()=>person.name,(newValue,oldValue)=>{console.log(newValue,oldValue)
})
watch([()=>person.name,()=>person.car.c1],(newValue,oldValue)=>{console.log('person变化了',oldValue,newValue)
},{deep:true})

案例:watchEffect监视ref变量的值

let tempature = ref(0)
let height = ref(10)
watchEffect(()=>{if(tempature.value > 20 || height.value > 30 ){console.log("aa")}
})

案例:defineExpose通信

  <Person ref="ren"/>
<script lang="ts" setup name="App">import {ref} from 'vue'//ts或者jsimport Person from './components/Person.vue'let title2 = ref()let ren = ref()function showLog(){console.log(ren.value)}
</script>
<script lang="ts" setup name="Person5467">import {ref,defineExpose} from 'vue'let title2 = ref()let a = ref(0)let b = ref(0)let c = ref(0)function showLog() {console.log("Person.Log")}defineExpose({a,b,c})</script>

案例:组件通信

<script lang="ts" setup name="App">
import {reactive} from 'vue'
//ts或者js
let x = 4
import {type Persons} from '@/types'
import Person from './components/Person.vue'
let personList = reactive<Persons>([{id:'001',name:'张三',age:13},{id:'002',name:'李四',age:20},{id:'003',name:'王五',age:22},
])</script>
<script lang="ts" setup name="Person5467">
import type { Persons } from '@/types';
import { defineProps,withDefaults } from 'vue';
//   接收list
//   defineProps(['list'])
//接收list+限制类型    
// defineProps<{list:Persons}>()
//接收list+限制类型+限制必要性+指定默认值
withDefaults(defineProps<{list?:Persons}>(),{list:()=>[{id:'1',name:'张三',age:12}]
})
</script>

案例:状态与钩子函数

<script lang="ts" setup name="Person5467">
import { onBeforeMount, onBeforeUnmount, onBeforeUpdate, onMounted, onUnmounted, onUpdated, ref } from 'vue';let sum = ref(0)function add(){sum.value += 1}//挂载前onBeforeMount(()=>{console.log('挂载前')})//挂载后onMounted(()=>{console.log("已挂载")})//更新前onBeforeUpdate(()=>{console.log("更新前")})//更新完毕onUpdated(()=>{console.log("更新完毕")})//卸载前onBeforeUnmount(()=>{console.log("卸载前")})//卸载完毕onUnmounted(()=>{console.log("卸载完毕")})
</script>

案例:Component声明与使用

ts文件

import { ref } from "vue"export default function (){let sum =ref(0)function add(){sum.value += 1}return {sum,add}
}

vue文件组件加载

<template><Person/>
</template><script lang="ts">import Person from "./components/Person.vue"export default {name:'App',components:{Person} //注册组件}
</script>

vue文件:ts文件加载

<script lang="ts" setup name="Person5467">import useDog from '@/hooks/useDog'import useSum from '@/hooks/useSum'const {sum,add} = useSum()const {dogList,getDog} = useDog()
</script>

案例:创建路由器

import {createRouter, createWebHistory} from 'vue-router'
import Home from '@/pages/Home.vue'
import News from '@/pages/News.vue'
import About from '@/pages/About.vue'
//第二部:创建路由器
const router = createRouter({history:createWebHistory(),routes:[{path:'/home',component:Home},{path:'/about',component:About},{path:'/news',component:News},]
})
export default router
<template><div class="app"><h2 class="title">Vue路由测试</h2><div class="navigate"><RouterLink  to="/home" active-class="active">首页</RouterLink><RouterLink to="/news" active-class="active">新闻</RouterLink><RouterLink  :to="{path:'/about'}" active-class="active">关于</RouterLink></div><div class="main-content"><RouterView></RouterView></div></div>
</template>

案例:创建子路由

import {createRouter, createWebHistory} from 'vue-router'import Home from '@/pages/Home.vue'
import News from '@/pages/News.vue'
import About from '@/pages/About.vue'
import Detail from '@/pages/Detail.vue'
//第二部:创建路由器
const router = createRouter({history:createWebHistory(),routes:[{path:'/home',component:Home},{path:'/about',component:About},{path:'/news',component:News,children:[{path:'detail',component:Detail}]},]
})
export default router

案例:路由传参

<RouterLink 
:to = "{name:'detail',params:{id:news.id,title:news.title,content:news.content,}
}">{{ news.title }}</RouterLink>

案例:路由props

<script setup lang="ts" name="News">
import { useRoute } from 'vue-router';defineProps(['id','title','content'])
</script>

案例:路由重定向

const router = createRouter({history:createWebHistory(),routes:[{path:'/home',component:Home},{path:'/about',component:About},{path:'/news',component:News,children:[{name:'detail',path:'detail/:id/:title/:content',component:Detail,//第一种写法,将路由收到的所有params参数作为props传给路由组件props:true,//第二种写法:可以自己决定讲什么作为参数// props(to) {//     console.log(to.query.id,to.query.title,to.query.content)//     return to.query// },}]},{path:'/',redirect:'/home'}]
})

案例:store

ts文件

import { defineStore } from "pinia"export const useCountStore = defineStore('count',{state() {return {sum:6}}
})
<script setup lang="ts" name="Count">
import { ref } from 'vue';
import { useCountStore } from '@/store/count'
const countStore = useCountStore()
//reactive 里面包含ref,就不需要拆包value
console.log('@@@',countStore.sum)let n = ref(1)function add() {}function minus() {}
</script>

案例:数据修改

<script setup lang="ts" name="Count">
import { ref } from 'vue';
import { useCountStore } from '@/store/count'
const countStore = useCountStore()
//reactive 里面包含ref,就不需要拆包value
console.log('@@@',countStore.sum)let n = ref(1)function add() {// countStore.sum += 1//第二种改变方式// countStore.$patch({//     sum:888,//     school:'尚硅谷',//     address:'北京'// })//第三宗countStore.increment(n.value)}
</script>
import { defineStore } from "pinia"export const useCountStore = defineStore('count',{actions:{increment(value: number){console.log("increment被调用了",value)this.sum += value}},state() {return {sum:6,school:'atguigu',address:'hong'}}
})

案例:pina中storeToRefs

<script setup lang="ts" name="Count">
import { ref } from 'vue';
import { useCountStore } from '@/store/count'
import { storeToRefs } from 'pinia';
const countStore = useCountStore()
//reactive 里面包含ref,就不需要拆包value
console.log('@@@',countStore.sum)
let n = ref(1)
const {school,address,sum} = storeToRefs(countStore)
function add() {countStore.increment(n.value)
}</script>
import { defineStore } from "pinia"
export const useCountStore = defineStore('count',{actions:{increment(value: number){console.log("increment被调用了",value)this.sum += value}},state() {return {sum:6,school:'atguigu',address:'hong'}}
})

案例:pinia进行数据改变

import { defineStore } from "pinia"export const useCountStore = defineStore('count',{actions:{increment(value: number){console.log("increment被调用了",value)this.sum += value}},state() {return {sum:6,school:'atguigu',address:'hong'}},getters:{bigSum:state =>state.sum * 10,upperSchool():string{return this.school.toUpperCase()}}})

案例:defineProps

Father.vue

<template><div class="father"><h3>父组件</h3><h4>父亲的玩具:{{ car }}</h4><h4>儿子的玩具:{{ toy }}</h4><Child :car="car" :sendToy="getToy"/></div>
</template><script setup lang="ts" name="Father">import Child from './Child.vue'import {ref} from 'vue'let car = ref('帕拉梅拉')let toy = ref('')function getToy(v:string){toy.value = v}
</script>
<style scoped>.father {background-color: rgb(165,164,164);padding: 20px;border-radius: 10px;}
</style>

Child.vue

<template><div class="child"><h3>子组件</h3><h4>儿子的玩具:{{   toy }}</h4><h4>父亲的玩具:{{ car }}</h4><button @click="sendToy(toy)">玩具给父亲</button></div>
</template><script setup lang="ts" name="Child">import {ref} from 'vue'let toy = ref('奥特玛')defineProps(['car','sendToy'])function send() {}
</script><style scoped>.child {background-color: skyblue;padding:10px;box-shadow: 0 0 10px black;border-radius: 10px;}
</style>

案例:shallowRef

<template><div class="app"><h3>求和为:{{ sum }}</h3><h3>名字为:{{ person.name }}</h3><h3>年龄为:{{ person.age }}</h3><h3>车子为{{car}}</h3><button @click="changeSum">sum+1</button><button @click="changeName">修改名字</button><button @click="changeAge">修改年龄</button><button @click="changePerson">修改整个人</button><span>|</span><button @click="changeBrand">修改品牌</button><button @click="changeColor">修改品牌</button><button @click="changeEngine">修改品牌</button></div></template><script lang="ts" setup name="App">import {reactive, ref, shallowReactive, shallowRef} from 'vue'//shallowRef只关心浅层次,也就是第一层**.value//实战中更关心整体替换,下面是一串官方的话//通过使用shallowRef()和shallowReactive()来绕开深度相应,//浅层式api创建的状态只在其顶层是响应式的,对所有深层的对象不会做任何处理//避开了对每一个内部属性做响应式所带来的性能成本,这使得//属性的访问变得更快,可提升性能let sum = shallowRef(0)let person = shallowRef({name:'张三',age:18})let car = shallowReactive({brand:'帕拉梅拉',options:{engine:'v8',color:'purple'}})function changeSum(){sum.value += 1}function changeName(){person.value.name += '李四'}function changeAge(){person.value.age += 1}function changePerson(){person.value.age += 3person.value.name += '王五'}function changeBrand(){car.brand = "法拉利"}function changeColor(){car.options.color = "blue"}function changeEngine(){car.options.engine = "v12"}
</script><style scoped>.app{background-color: #ddd;border-radius: 10px;box-shadow: 0 0 10px;padding: 10px;}
</style>

案例:readOnly与shallowReadOnly

<template><div class="app"><h3>当前sum1为:{{ sum1 }}</h3><h3>当前sum2为:{{ sum2 }}</h3><h3>当前车为{{car1}}</h3><h3>当前车为{{car2}}</h3><button @click="changeSum1">点我sum1+1</button><button @click="changeSum2">点我sum2+1</button><button @click="changeBrand">修改品牌</button><button @click="changeColor">修改颜色</button><button @click="changePrice">修改价格</button></div></template><script lang="ts" setup name="App">import {ref,readonly,reactive, shallowReadonly} from 'vue';let sum1 = ref(0)let sum2 = readonly(sum1) //深只读副本let car1 = reactive({brand:'奔驰',options:{color:'红色',price:100}})let car2 = shallowReadonly(car1) //只用于对象的顶层属性function changeSum1(){sum1.value += 1}function changeSum2() {sum2.value += 1}function changeBrand() {car2.brand = '帕拉梅拉'}function changeColor() {car2.options.color = 'purple'   }function changePrice() {car2.options.price = 200}
</script><style scoped>.app{background-color: #ddd;border-radius: 10px;box-shadow: 0 0 10px;padding: 10px;}
</style>

案例:markRaw与toRaw

<template>
<!-- toRaw:用于获取一个响应式对象的原始对象, -->
<!-- toRaw返回的对象不再是响应式的,不会触发视图更新 -->
<!-- markRow:标记一个对象,使其永远不会变成响应式的 -->
<h2>姓名:{{ person2.name }}</h2>
<h2>年龄:{{ person2.age }}</h2>
<h2>一个车子{{ car2 }}</h2>
<button @click="person2.age += 1">修改年龄</button>
<button @click="changePerson(person2)">修改整个人</button>
<button @click="car2.price += 10">修改车的价格</button>
</template><script lang="ts" setup name="App">import {markRaw, reactive,toRaw} from 'vue'let person2 = reactive({name:'tony',age:17})let rawPerson = toRaw(person2)function changePerson(p){p.age +=1 p.name = 'tom'}let car = markRaw({brand:'帕拉梅拉',price:100})let car2 = reactive(car)</script><style scoped>.app{background-color: #ddd;border-radius: 10px;box-shadow: 0 0 10px;padding: 10px;}
</style>

案例:customRef

自定义ref useMsg.ts

import { customRef } from 'vue';export default function(initValue:string,delay:number){let timer:numberlet msg = customRef((track,trigger)=>{return {get() {track()return initValue},set(value){clearTimeout(timer)timer = setTimeout(()=>{console.log('set',value)initValue = valuetrigger()},delay)}}})return {msg}
}

.vue

<template><h3>我想要的:{{ msg }}</h3><input type="text" v-model="msg"></template><script lang="ts" setup name="App">
import useMsg from './useMsg';let {msg} = useMsg('你好',2000)</script><style scoped>.app{background-color: #ddd;border-radius: 10px;box-shadow: 0 0 10px;padding: 10px;}
</style>

案例:telePort

Modal.vue

<template><button @click="isShow=true">暂时弹窗</button><div class="modal" v-show="isShow"><h2>我是标题</h2><p>我是内容</p><button>我是弹窗</button></div><!-- 创建一个自定义的ref,并对其依赖项追踪和更新触发进行逻辑控制 -->
</template><script lang="ts" setup name="Modal">
import { ref } from 'vue';let isShow = ref(false)</script><style scoped>.modal{width: 200px;height: 150px;background-color: skyblue;border-radius: 10px;box-shadow: 0 0 10px;padding: 5px;text-align: center;position: fixed;left:50%;top:20px;margin-left: -100px;}
</style>

App.vue

<template><div class="outer"><h2>我是app组件</h2><img src="./tmp.png" alt=""><br><Modal /></div></template><script lang="ts" setup name="App">import Modal from './Modal.vue'
</script><style scoped>
.outer {background-color: #ddd;border-radius: 10px;padding: 5px;box-shadow: 0 0 10px;
}
img {width: 400px;filter: saturate(280%);
}
</style>

案例:fallback

<template><div class="app"><h2>我是app组件</h2><Suspense><template v-slot:default><Child/></template><template v-slot:fallback><h2>加载中....</h2></template></Suspense></div></template><script lang="ts" setup name="App">import Child from './Child.vue'import { Suspense } from 'vue';
</script><style scoped>.app {background-color: #add;border-radius: 10px;padding: 10px;box-shadow: 0 0 10px;}
</style>
<template><div class="child"><img :src="message"></div></template><script lang="ts" setup name="Child">import {ref} from 'vue'import axios from 'axios';let sum = ref(0)let {data:{message}} = await axios.get('https://dog.ceo/api/breed/pembroke/images/random')console.log(message)
</script><style scoped>.child {background-color: #bce;border-radius: 10px;padding: 10px;box-shadow: 0 0 10px;}
</style>

app相关的操作

main.ts

import {createApp} from 'vue'
import App from './App.vue'
import Hello from './Hello.vue'const app = createApp(App)app.component('Hello',Hello)
app.config.globalProperties.x = 99
declare module 'vue' {interface ComponentCustomProperties {x:number}
}
app.directive('beauty',(element,{value})=>{element.innerText += valueelement.style.color = 'green'element.style.backgroundColor = 'yellow'
})
app.mount('#app')
// setTimeout(()=>{
//     app.unmount()
// },2000)

App.vue

<template><div class="app"><h2>我是app组件</h2><Hello/><Child/></div></template><script lang="ts" setup name="App">import Child from './Child.vue'</script><style scoped>.app {background-color: #add;border-radius: 10px;padding: 10px;box-shadow: 0 0 10px;}
</style>

Child.vue

<template><div class="child"><h2>我是child组件</h2><h3 >当前求和为:{{ sum }}</h3><h3 v-beauty="sum">差值</h3><Hello/></div></template><script lang="ts" setup name="Child">import {ref} from 'vue'let sum  = ref(3)
</script><style scoped>.child {background-color: #bce;border-radius: 10px;padding: 10px;box-shadow: 0 0 10px;}
</style>

案例:作用域插槽

Father.vue

<template><div class="father"><h3>父组件</h3><div class="content"><Game><template v-slot:games="params"><ul><li v-for="g in params.games" :key="g.id">{{ g.name }}</li></ul></template></Game><Game v-slot:games="{games}"><ol><li v-for="g in games" :key="g.id">{{ g.name }}</li></ol></Game><Game v-slot:games="params"><h3 v-for="g in params.games" :key="g.id">{{ g.name }}</h3></Game></div></div>
</template><script setup lang="ts" name="Category">
import Game from './Game.vue';</script><style scoped>
.father {background-color: rgb(165, 164, 164);padding: 10px;border-radius: 10px;
}.content {display: flex;justify-content: space-evenly;
}img,
video {width: 100%;
}
</style>

Child.vue

<template><div class="game"><h2>游戏列表</h2><slot name ="games" :games="games"></slot></div>
</template><script setup lang="ts" name="Game">import {reactive } from 'vue';let games = reactive([{id:'001',name:'英雄联盟'},{id:'002',name:'王者农药'},{id:'003',name:'红色警戒'},{id:'004',name:'斗罗大陆'},])
</script><style scoped>
.game {width: 200px;height: 300px;background-color: skyblue;border-radius: 10px;box-shadow: 0 0 10px;
}
h2 {background-color: orange;text-align: center;font-size: 14px;font-weight: 800;}
</style>

案例:组件通信1

Father.vue

<template><div class="father"><h3>父组件</h3><h4>父亲的玩具:{{ car }}</h4><h4>儿子的玩具:{{ toy }}</h4><Child :car="car" :sendToy="getToy"/></div>
</template><script setup lang="ts" name="Father">import Child from './Child.vue'import {ref} from 'vue'let car = ref('帕拉梅拉')let toy = ref('')function getToy(v:string){toy.value = v}
</script>
<style scoped>.father {background-color: rgb(165,164,164);padding: 20px;border-radius: 10px;}
</style>

Child.vue

<template><div class="child"><h3>子组件</h3><h4>儿子的玩具:{{   toy }}</h4><h4>父亲的玩具:{{ car }}</h4><button @click="sendToy(toy)">玩具给父亲</button></div>
</template><script setup lang="ts" name="Child">import {ref} from 'vue'let toy = ref('奥特玛')defineProps(['car','sendToy'])function send() {}
</script><style scoped>.child {background-color: skyblue;padding:10px;box-shadow: 0 0 10px black;border-radius: 10px;}
</style>

案例:组件通信2-customEvent


<template><div class="father"><h3>父组件</h3><h4 v-show="toy">子给的玩具:{{ toy }}</h4><!-- //  <button @click="test()">点我</button> --><Child @send-toy="saveToy"/></div>  
</template><script setup lang="ts" name="Father">import Child from './Child.vue'import {ref} from 'vue'let toy = ref('')function saveToy(val:string) {console.log('saveToy',val)toy.value = val}
</script><style scoped >.father {background-color: rgb(165,164,164);padding: 20px;border-radius: 10px;}.father button {margin-right: 5px;}
</style>
<template><div class="child"><h3>子组件</h3><h4>玩具:{{ toy }}</h4><button @click="emit('send-toy',toy)">测试</button></div>
</template><script setup lang="ts" name="Child">
import { ref } from 'vue';let toy = ref('奥特玛')
const emit = defineEmits(['send-toy'])
</script><style scoped>.child {background-color: rgb(76,209,76);padding: 10px;margin-top: 10px;box-shadow: 0 0 10px black;border-radius: 10px;}
</style>

案例:组件通信3-mitt

<template><div class="father"><h3>父组件</h3><Child1 /><Child2 />
</div>
</template><script setup lang="ts" name="Father">import Child2 from './Child2.vue';import Child1 from './Child1.vue';
</script><style scoped>.father{background-color: yellow;padding: 20px;border-radius: 10px;}
</style>
<template><div class="child1"><h3>子组件1</h3><h4>玩具:{{ toy }}</h4><button @click="emitter.emit('send-toy',toy)">玩具给弟弟</button></div>
</template><script setup lang="ts" name="Child1">import {ref} from 'vue'import emitter from '@/utils/emitter';let toy = ref('奥特曼')
</script><style scoped>.child1 {margin-top: 50px;background-color: skyblue;padding: 10px;box-shadow: 0 0 10px black;border-radius: 10px;}.child1 button {margin-right: 10px;}
</style>
<template><div class="child2"><h3>子组件2</h3><h4>电脑::{{ computer }}</h4><h4>玩具:{{ toy }}</h4></div>
</template><script setup lang="ts" name="Child2">
import { onUnmounted, ref } from 'vue';let computer = ref('联想')
let toy = ref('')
//数据
import emitter from '@/utils/emitter';
emitter.on('send-toy', (val: any) => {console.log('Received value:', val);  // 确认 val 的值toy.value = val;  // 更新 toy 的值
});
onUnmounted(()=>{emitter.off('send-toy')
})
</script>
<style scoped>
.child2 {margin-top: 50px;background-color: rgb(133, 112, 218);padding: 10px;box-shadow: 0 0 10px black;border-radius: 10px;
}.child1 button {margin-right: 10px;}
</style>

案例:组件通信4

<template><div class="father"><h3>父组件</h3><!---v-model 用在html标签上--><!-- <input type="text" v-model="username"> --><!--<input type="text" :value="username" @input="username"=(<HTMLInputElement>$event.target).value--><!-- <AtHome :modelValue="username" @update:modelValue="username = $event"/>--><AtHome v-model:name="username" v-model:pwd="password"/><!-- <AtHome v-model="username" /> --></div>
</template><script setup lang="ts" name="Father">
import { ref } from "vue"
import AtHome from "./AtHome.vue";
let username = ref('zhangsan')
let password = ref('123456')
</script><style scoped>
.father {padding: 20px;background-color: rgb(165, 164, 164);border-radius: 10px;
}
</style>
<template><input type="text":value="name"@input="emit('update:name',(<HTMLInputElement>$event.target).value)"><input type="text":value="pwd"@input="emit('update:pwd',(<HTMLInputElement>$event.target).value)">
</template><script setup lang="ts" name="AtHome">defineProps(['name','pwd'])const emit = defineEmits(['update:name','update:pwd'])
</script><style scoped>
input {border:2px solid black;background-image: linear-gradient(45deg,red,yellow,green);height:30px;font-size:20px;color: white;
}
</style>

案例:组件通信5

<template><div class="grand-child"><h3>孙组件</h3><h4>{{ a }}</h4><h4>{{ b }}</h4><h4>{{ c }}</h4><h4>{{ d }}</h4><button @click="updateA(1)">点我更新爷爷</button></div>
</template><script setup lang="ts" name="GrandChild">
defineProps(['a','b','c','d','updateA'])
</script><style scoped>.grand-child {background-color: orange;padding:20px;margin-top: 20px;border-radius: 10px;box-shadow: 0 0 10px black;}
</style>
<template><div class="father"><h3>父组件</h3><h4>a:{{ a }}</h4><h4>b:{{ b }}</h4><h4>c:{{ c }}</h4><h4>d:{{ d }}</h4><Child :a="a" :b="b" :c="c" :d="d" :updateA="updateA"/></div>
</template><script setup lang="ts" name="Father">import Child from "./Child.vue"import {ref} from 'vue'let a = ref(1)let b = ref(2)let c = ref(3)let d = ref(4)function updateA(val:number) {a.value += val}
</script><style scoped>.father {background-color: rgb(165,164,164);padding:20px;border-radius: 10px;}
</style>
<template><div class="child"><h3>子组件</h3><GrandChild v-bind="$attrs"/></div>
</template><script setup lang="ts" name="Child">import GrandChild from "./GrandChild.vue"
</script><style scoped>.child {background-color: skyblue;padding:20px;margin-top: 20px;border-radius: 10px;box-shadow: 0 0 10px black;}
</style>

案例:组件通信6

<template>
<div class="father"><h3>父组件</h3><h4>房产:{{ house }}</h4><button @click="changeToy">修改child1玩具</button><Child1 ref="c1"/><button @click="changeToy2">修改child2玩具</button><button @click="getAllChild($refs)">获取所有子组件实例对象</button><Child2 ref="c2"/>
</div>
</template>
<script setup lang="ts" name="Father">import Child1 from './Child1.vue'import Child2 from './Child2.vue'import { ref } from 'vue'let house = ref(4)let c1 = ref()function changeToy(){console.log(c1.value)c1.value.toy = "杰瑞"}let c2 = ref()function changeToy2(){c2.value.computer = "华硕"}function getAllChild(obj:any){for (let key in obj){obj[key].book +=3}}defineExpose({house})
</script><style scoped >.father {background-color: rgb(165,164,164);padding:20px;border-radius: 10px;}
</style>
<template><div class="child1"><h3>子组件1</h3><h4>儿子1的玩具:{{ toy }}</h4><h4>儿子1的书籍:{{ book }}</h4><button @click="minusHouse($parent)">干掉父亲的一套房产</button></div>
</template>
<script setup lang="ts" name="Child2">
import { ref } from 'vue';let toy = ref('汤姆')
let book = ref(3)
defineExpose({toy,book})
function minusHouse(parent:any) {parent.house -= 1
}
</script><style scoped>
.child1 {margin-top: 20px;padding: 20px;background-color: skyblue;border-radius: 10px;box-shadow: 0 0 10px black;
}
</style>
<template><div class="child2"><h3>子组件2</h3><h4>儿子2的玩具:{{ computer }}</h4><h4>儿子2的书籍:{{ book }}</h4></div></template><script setup lang="ts" name="Child2">import { ref } from 'vue';let computer = ref('联想')
let book = ref(6)
defineExpose({computer,book})</script><style scoped >.child2 {margin-top: 20px;padding:20px;background-color: orange;border-radius: 10px;box-shadow: 0 0 10px black;}</style>

案例:组件通信7

<template><div class="father"><h3>父组件</h3><h4>银子:{{ money }}</h4><h4>车子: 一辆{{ car.brand }},价值{{ car.price }}万元</h4><Child/></div></template><script setup lang="ts" name="Father">import Child from './Child.vue'import {ref,reactive, provide} from 'vue'let money = ref(100)let car = reactive({brand:'奔驰',price:100,})function updateMoney(val:number){money.value -= val}provide('moneyContext',{money,updateMoney})provide('che',car)
</script><style scoped>.father {background-color: rgb(165,164,164);padding:20px;border-radius: 10px;}
</style>
<template><div class="child"><h3>子组件</h3><GrandChild/></div></template><script setup lang="ts" name="Child">import GrandChild from './GrandChild.vue'
</script><style scoped>.father {margin-top: 20px;background-color: skyblue;padding:20px;border-radius: 10px;box-shadow: 0 0 10px black;}
</style>
<template><div class="grand-child"><h3>我是孙组件</h3><h4>:{{ money }}</h4><button @click="updateMoney(6)">花爷爷的钱</button></div>
</template><script setup lang="ts" name="GrandChild">
import { inject } from 'vue';let {money,updateMoney} = inject('moneyContext',{money:0,updateMoney:(parma:number)=>{}})
let car = inject('car',{brand:'未知',price:0})
</script><style scoped>
.grand-child{background-color: orange;padding:20px;border-radius: 10px;box-shadow: 0 0 10px black;
}
</style>

案例:组件通信8

<template><div class="father"><h3>父组件</h3><div class="content"><Category ><template v-slot:s2><ul><li v-for="g in games" :key="g.id">{{g.name}}</li></ul></template><template v-slot:s1><h2>热门游戏列表</h2></template></Category><Category ><template v-slot:s2><img src="./tmp.png" alt="111"></template><template v-slot:s1><h2>今日美食城市</h2></template></Category><Category ><template #s1><h2>今日影视推荐</h2></template><template #s2><video src="./1.mp4" alt="222" controls></video></template></Category></div></div>
</template><script setup lang="ts" name="Category">
import { reactive,ref } from 'vue';
import Category from './Category.vue';
let games = reactive([{id:'001',name:'英雄联盟'},{id:'002',name:'王者农药'},{id:'003',name:'红色警戒'},{id:'004',name:'斗罗大陆'},
])
let imgUrl = ref('@/09_slot/tmp.png')
let mp4Url = ref('./1.mp4')</script><style scoped>/* .category {background-color: rgb(165,164,164);padding:10px;border-radius: 10px;} */.content {display: flex;justify-content: space-evenly;}img,video {width: 100% ;}h2 {background-color: orange;text-align: center;font-size: 14px;font-weight: 800;}
</style>
<template><div class="category"><h2>{{ title }}</h2><slot name="s1">默认内</slot><slot name="s2">默认内</slot></div>
</template><script setup lang="ts" name="Category">
defineProps(['title'])
</script><style scoped>.category {background-color: skyblue;border-radius: 10px;box-shadow: 0 0 10px;padding:10px;width:200px;height:300px;}h2 {background-color: orange;text-align: center;font-size: 14px;font-weight: 800;}
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/20582.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

初学PADS使用技巧笔记(也许会继续更新)

操作意图&#xff1a;网上找某个芯片封装又不想自己画&#xff0c;再加上没经验&#xff0c;怎么办&#xff1f; 就以AC-DC芯片PN8036为例&#xff0c;打开嘉立创的的DFM&#xff0c;打开立创商城&#xff0c;输入PN8036&#xff0c;点击数据手册&#xff0c;然后点击直接打开…

解锁机器学习核心算法 | 随机森林算法:机器学习的超强武器

一、引言 在机器学习的广阔领域中&#xff0c;算法的选择犹如为一场冒险挑选趁手的武器&#xff0c;至关重要。面对海量的数据和复杂的任务&#xff0c;合适的算法能够化繁为简&#xff0c;精准地挖掘出数据背后隐藏的模式与价值。机器学习领域有十大核心算法&#xff0c;而随…

LeetCode每日精进:225.用队列实现栈

题目链接&#xff1a;225.用队列实现栈 题目描述&#xff1a; 请你仅使用两个队列实现一个后入先出&#xff08;LIFO&#xff09;的栈&#xff0c;并支持普通栈的全部四种操作&#xff08;push、top、pop 和 empty&#xff09;。 实现 MyStack 类&#xff1a; void push(int x…

二.数据治理流程架构

1、数据治理流程架构核心思想&#xff1a; 该图描绘了一个以数据标准规范体系为核心&#xff0c;大数据生命周期管理为主线&#xff0c;数据资源中心为依托&#xff0c;并辅以数据质量管理和大数据安全与隐私管理的数据治理流程架构。它旨在通过规范化的流程和技术手段&#x…

java_使用Spring Cloud Gateway + nacos实现跨域访问

Spring Cloud Gateway简介 Spring cloud gateway是spring官方基于Spring 5.0、Spring Boot2.0和Project Reactor等技术开发的网关&#xff0c;Spring Cloud Gateway旨在为微服务架构提供简单、有效和统一的API路由管理方式&#xff0c;Spring Cloud Gateway作为Spring Cloud生…

Linux中安装open-webui报sqlite版本低的解决办法

almalinux中安装好open-webui&#xff0c;启动服务时报如下错&#xff1a; RuntimeError: [91mYour system has an unsupported version of sqlite3. Chroma requires sqlite3 > 3.35.0.[0m [94mPlease visit https://docs.trychr…

基于SpringBoot+Vue的老年人体检管理系统的设计与实现(源码+SQL脚本+LW+部署讲解等)

专注于大学生项目实战开发,讲解,毕业答疑辅导&#xff0c;欢迎高校老师/同行前辈交流合作✌。 技术范围&#xff1a;SpringBoot、Vue、SSM、HLMT、小程序、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、安卓app、大数据、物联网、机器学习等设计与开发。 主要内容&#xff1a;…

【AI视频】Runway注册、基本设置、主界面详解

博客主页&#xff1a; [小ᶻ☡꙳ᵃⁱᵍᶜ꙳] 本文专栏: AI视频 | Runway 文章目录 &#x1f4af;前言&#x1f4af;Runway的正确启动方式推荐使用Google Chrome打开Chrome翻译 &#x1f4af;Runway的注册&#x1f4af;My Account&#xff08;我的账户&#xff09;General&a…

大数据的特点

高速、多样性、大量、低价值密度 大数据的应用场景 视频推荐&#xff0c;电商推荐&#xff0c;零售&#xff0c;金融 发展脉络 1.单机时代 2.大数据时代-分布式处理 Hadoop的优势 高可靠性、高拓展性、高效性、 高容错性

P8752 [蓝桥杯 2021 省 B2] 特殊年份——string提取索引转换为值

这里写目录标题 链接题目代码大佬解答string提取索引转换为值 链接 P8752 [蓝桥杯 2021 省 B2] 特殊年份 题目 代码 #include <iostream> #include <vector> #include <string> #include <algorithm> #include <math.h> #include <queue&g…

使用SHOW PROCESSLIST和SHOW ENGINE INNODB STATUS排查mysql锁等待问题

现象&#xff1a; mysql 查某表一直不能结束&#xff0c;查别的表没有问题。已知之前刚刚alter此表想把它的一个字段长度增长&#xff0c;但是这个操作一直没有结束。现在应该怎么办? 方案: 使用 SHOW PROCESSLIST; 查看当前所有活动的SQL线程&#xff0c;找出是否有长时间…

Unity UI个人总结

个人总结&#xff0c;太简单的直接跳过。 一、缩放模式 1.固定像素大小 就是设置一个100x100的方框&#xff0c;在1920x1080像素下在屏幕中长度占比1/19&#xff0c;在3840x2160&#xff0c;方框在屏幕中长度占比1/38。也就是像素长款不变&#xff0c;在屏幕中占比发生变化 2.…

Jmeter如何计算TPS

1.在jmeter中计算出接口请求的个数 1175 1172 1172 174 200 416 384 1174 5867 2.计算接口平均响应时间 计算每个接口的请求次数乘以平均响应时间&#xff0c;所有接口相加&#xff0c;然后除以所有接口的数量总和&#xff0c;得到接口的平均响应时间 (1175*18191172*…

【R语言】回归分析与判别分析

一、线性回归分析 1、lm()函数 lm()函数是用于拟合线性模型&#xff08;Linear Models&#xff09;的主要函数。线性模型是一种统计方法&#xff0c;用于描述一个或多个自变量&#xff08;预测变量、解释变量&#xff09;与因变量&#xff08;响应变量&#xff09;之间的关系…

上线了一个微软工具(免费),我独自开发,本篇有源码

各位读者老爷们好。今天给大家推荐一个我刚上线微软商店的免费工具。 起因是有一些看似简单的文本处理功能,有时却很难找到针对性的工具。 比如我前几天有需求将一个巨大的TXT文件切割成多个指定大小的小TXT,却发现很难找到趁手的批量工具。 没有,那我就写一个。 python写…

vue elementui select下拉库组件鼠标移出时隐藏下拉框

方案&#xff1a; select 监听 mouseleave事件&#xff0c;当鼠标离开时通过唯一标识ref设置select 下拉框隐藏&#xff0c;并做失焦 <el-select v-model"value" :popper-append-to-body"false" class"select_drop_inner" size"s…

Docker 安装和配置 Nginx 详细图文教程

&#x1f680; 作者主页&#xff1a; 有来技术 &#x1f525; 开源项目&#xff1a; youlai-mall ︱vue3-element-admin︱youlai-boot︱vue-uniapp-template &#x1f33a; 仓库主页&#xff1a; GitCode︱ Gitee ︱ Github &#x1f496; 欢迎点赞 &#x1f44d; 收藏 ⭐评论 …

Golang学习笔记_34——组合模式

Golang学习笔记_31——原型模式 Golang学习笔记_32——适配器模式 Golang学习笔记_33——桥接模式 文章目录 一、核心概念1. 定义2. 解决的问题3. 核心角色4. 类图 二、特点分析三、适用场景1. 文件系统2. 图形界面3. 组织架构 四、代码示例&#xff08;Go语言&#xff09;五、…

LKT4202UGM新一代安全认证加密芯片,守护联网设备和服务安全

LKT4202UGM是提供身份验证、机密性和平台完整性服务的安全元件产品&#xff0c;可保护原始设备制造商免受克隆、伪造、恶意软件注入和未经授权生产的侵害。LKT安全元件经过最为严格的安全认证&#xff0c;可提供一站式解决方案。 为满足市场对LKT产品的需求&#xff0c;凌科芯…

人工智能之目标追踪DeepSort源码解读(yolov5目标检测,代价矩阵,余弦相似度,马氏距离,匹配与预测更新)

要想做好目标追踪,须做好目标检测,所以这里就是基于yolov5检测基础上进行DeepSort,叫它为Yolov5_DeepSort。整体思路是先检测再追踪,基于检测结果进行预测与匹配。 一.参数与演示 这里用到的是coco预训练人的数据集&#xff1a; 二.针对检测结果初始化track 对每一帧数据都输出…