vue3组件通信学习笔记

1、Prop

父组件

<template>
    <div class="parent">
        <h1>我是父元素</h1>
        <Child :msg="msg"></Child>
    </div>
</template>

<script setup>
import Child from './Child.vue'
let msg = ref('我是父组件的数据')
</script>

<style scoped>
.parent{
    width: 800px;
    height: 600px;
    background: skyblue;
}
</style>

子组件

<template>
    <div class="son">
        <h3>我是子元素</h3>
        <h4>{{ msg }}</h4>
    </div>
</template>

<script setup>
// 1、let props = defineProps(['msg'])
//2、defineProps(['msg'])
//3、
defineProps({
    msg:{
        type:String,
        default:'默认数据'
    }
})
</script>

<style scoped>
.son{
    width: 200px;
    height: 200px;
    background: pink;
}
</style>

在这里插入图片描述

2、自定义事件

父组件

<template>
    <div class="parent">
        <h1>我是父元素</h1>
        <Child :msg="msg" @getData="getData"></Child>
        <hr>
        <p>父组件接收数据:{{passStr}}</p>
    </div>
</template>

<script setup>
import Child from './Child.vue'
let msg = ref('我是父组件的数据');
let passStr = ref('')
const getData = (e)=>{
    passStr.value=e
}
</script>

<style scoped>
.parent{
    width: 800px;
    height: 600px;
    background: skyblue;
}
</style>

子组件

<template>
    <div class="son">
        <h3>我是子元素</h3>
        <h4>{{ msg }}</h4>
        <button @click="passData('我是子组件数据')">给父组件传递参数</button>
    </div>
</template>

<script setup>
// 1、let props = defineProps(['msg'])
//2、defineProps(['msg'])
//3、
defineProps({
    msg:{
        type:String,
        default:'默认数据'
    }
})
let $emit = defineEmits('getData')
const passData = (msg)=>{
    $emit('getData',msg)
}
</script>

<style scoped>
.son{
    width: 200px;
    height: 200px;
    background: pink;
}
</style>

在这里插入图片描述

3、全局事件总线

1、安装

npm install --save mitt

2、新建utils/mitt.js

import mitt from 'mitt'
 
const emitter =new mitt()

export default emitter

3、使用

子组件1

<template>
    <div class="son">
        <h3>我是子元素</h3>
        <h4>{{ msg }}</h4>
        <button @click="passData('我是子组件数据')">给父组件传递参数</button>
        <button @click="passBusData('我是子组件1的数据')">给子组件2传递参数</button>
    </div>
</template>

<script setup>
import emitter from '../utils/mitt'
// 1、let props = defineProps(['msg'])
//2、defineProps(['msg'])
//3、
defineProps({
    msg:{
        type:String,
        default:'默认数据'
    }
})
let $emit = defineEmits('getData')
const passData = (msg)=>{
    $emit('getData',msg)
}
const instance = getCurrentInstance()
const passBusData = (msg) =>{
    emitter.emit("passBusData",msg)
}
</script>

<style scoped>
.son{
    width: 200px;
    height: 200px;
    background: pink;
}
</style>

子组件2

<template>
    <div class="child1">
        <h3>子组件2</h3>
        <p>{{msg}}</p>
    </div>
</template>

<script setup>
import emitter from '../utils/mitt'
let msg = ref('')
onMounted(() => {
  
    emitter.on("passBusData",(data) =>{
        msg.value=data
    })
});
</script>

<style  scoped>
.child1{
    width: 200px;
    height: 200px;
    background: hotpink;
}
</style>

在这里插入图片描述

4、v-model

1、绑定单个数据

父组件

<template>
    <div class="parent">
        <h1>父组件钱数:{{money}}</h1>
        <input type="number" v-model="money">
        <!-- props:父组件给子组件传值 -->
        <!-- <Child :modelValue="money" @update:modelValue="getData" /> -->
        <!-- 
            v-model组件身上使用
            1、相当于给子组件传递props[modelValue] = 1000
            2、给子组件绑定自定义事件update:modelValue
         -->
        <Child v-model="money" />
    </div>
</template>

<script setup>
import Child from './Child.vue';
import { ref } from 'vue';
// v-model 指令 收集表单数据,双向数据绑定
// v-model也可以实现组件之间通信,实现父子组件数据同步
// 父组件给子组件传数据 props
// 子组件给父组件传数据 自定义事件
let money = ref(1000);
const getData = (data)=>{
    money.value = data
}
</script>

<style lang="scss" scoped>
.parent{
    width:800px;
    height:500px;
    background: pink;
}
</style>

子组件

<template>
    <div class="child">
        <h3>子组件接收父组件钱数{{modelValue}}</h3>
        <button @click="handler">子组件改变数据</button>

    </div>
</template>

<script setup>
let props = defineProps(['modelValue']);
let emits = defineEmits(['update:modelValue']);
const handler = () => {
    emits('update:modelValue',props.modelValue+100)
}
</script>

<style lang="scss" scoped>
.child{
    width: 300px;
    height: 200px;
    background: skyblue;
}
</style>

在这里插入图片描述

2、绑定多个数据

父组件

<template>
    <div class="parent">
        <h2>父组件钱数:{{money}}当前页{{currentPage}}每页条数{{pageSize}}</h2>
        <input type="number" v-model="money">
        <!-- props:父组件给子组件传值 -->
        <!-- <Child :modelValue="money" @update:modelValue="getData" /> -->
        <!-- 
            v-model组件身上使用
            1、相当于给子组件传递props[modelValue] = 1000
            2、给子组件绑定自定义事件update:modelValue
         -->
        <Child v-model="money" />
        <Child2 v-model:currentPage="currentPage" v-model:pageSize="pageSize"></Child2>
    </div>
</template>

<script setup>
import Child from './Child.vue';
import Child2 from './Child2.vue';
import { ref } from 'vue';
// v-model 指令 收集表单数据,双向数据绑定
// v-model也可以实现组件之间通信,实现父子组件数据同步
// 父组件给子组件传数据 props
// 子组件给父组件传数据 自定义事件
let money = ref(1000);
const getData = (data)=>{
    money.value = data
}
//
let currentPage =ref(1)
let pageSize =ref(20)
</script>

<style lang="scss" scoped>
.parent{
    width:800px;
    height:600px;
    background: pink;
}
</style>

子组件

<template>
    <div class="child2">
        <h3>子组件2接收父组传递的数据{{currentPage}}{{ pageSize }}</h3>
        <button @click="emits('update:currentPage',currentPage+1)">改变父组件currentPage</button>
        <button @click="emits('update:pageSize',pageSize+10)">改变父组件pageSize</button>
    </div>
</template>

<script setup>
let props=defineProps(['currentPage','pageSize'])
let emits= defineEmits(['update:currentPage','update:pageSize'])
</script>

<style lang="scss" scoped>
.child2{
    width:300px;
    height:200px;
    background: hotpink;
}
</style>

在这里插入图片描述

5、useAttr

父组件

<template>
    <div class="parent">
        <h2>父组件</h2>
        <Child type="primary" title="编辑按钮" size="small" @click="handler" />
    </div>
</template>

<script setup>
import Child from './Child.vue';
const handler = ()=>{
    alert('点击了')
}
</script>

<style lang="scss" scoped>
.parent{
    width:800px;
    height:600px;
    background: pink;
}
</style>

子组件

<template>
    <div class="child">
        <h3>子组件</h3>
        <!-- <el-button :type="$attrs.type" :size="$attrs.size">按钮</el-button> -->
        <el-button :="$attrs">按钮</el-button>
    </div>
</template>

<script setup>
//引入useAttrs方法:获取组件标签身上属性与事件
let $attrs = useAttrs()

let props= defineProps(['title'])
console.log(props)
console.log($attrs)
//props和useAttrs方法都可以获取父组件传过来的属性和属性值
//但是props接收了useAttrs方法就获取不到了
</script>

<style lang="scss" scoped>
.child{
    width: 300px;
    height: 200px;
    background: skyblue;
}
</style>

在这里插入图片描述

6、ref与$parent

父组件

<template>
    <div class="parent">
        <h2>父组件钱数:{{money}}</h2>
        <button @click="borrowMoney">向子组件借100</button>
        <Child ref="child" />
    </div>
</template>

<script setup>
import Child from './Child.vue';
// ref可以获取真实的DOM节点,可以获取到组件真实VC
//$parent 可以在子组件内部获取父组件的实例
let money = ref(1000)

//向外暴露
defineExpose({
    money
})
//获取子组件实例
let child = ref();

//借钱
let borrowMoney = ()=>{
    money.value+=100;
    child.value.money-=100;
    //child.value.handler()
}
</script>

<style lang="scss" scoped>
.parent{
    width:800px;
    height:600px;
    background: pink;
}
</style>

子组件

<template>
    <div class="child">
        <h3>子组件钱数:{{money}}</h3>
        <button @click="handler($parent)">向父组件借钱100</button>
    </div>
</template>

<script setup>
let money = ref(1000)
//组件内部的数据是对外关闭的,别人不能访问
//如果想让外部访问需要通过defineExpose方法对外暴露
let handler = ($parent)=>{
    console.log('子组件事件');
    money.value+=100;
    $parent.money-=100
}
defineExpose({
    money,handler
})
</script>

<style lang="scss" scoped>
.child{
    width: 300px;
    height: 200px;
    background: skyblue;
}
</style>

在这里插入图片描述

7、Provide与Inject

父组件

<template>
    <div class="parent">
        <h2>父组件数据:{{car}}</h2>
        
        <Child ref="child" />
    </div>
</template>

<script setup>
import Child from './Child.vue';
//vue3提供provide(提供)与inject(注入)可以实现隔辈组件传递数据
let car = ref('法拉利');
//祖先组件给后代组件传递数据
//两个参数,第一个参数是提供数据的数据key
//第二个参数 祖先组件提供的数据
provide('car', car);
</script>

<style lang="scss" scoped>
.parent{
    width:800px;
    height:600px;
    background: pink;
}
</style>

子组件

<template>
    <div class="child">
        <h3>子组件</h3>
        <Grandson></Grandson>
    </div>
</template>

<script setup>
import Grandson from './Grandson.vue';
</script>

<style lang="scss" scoped>
.child{
    width: 300px;
    height: 200px;
    background: skyblue;
}
</style>

孙子组件

<template>
    <div class="grandson">
        <h5>孙子组件接收祖先组件传递的数据:{{car}}</h5>
        <button @click="updateData">更改祖先组件数据</button>
    </div>
</template>

<script setup>
//注入祖先组件提供的数据
//需要参数:即为祖先组件提供数据的key
let car = inject("car");
let updateData = ()=>{
    car.value='宝马'
}
</script>

<style lang="scss" scoped>
.grandson{
    width:200px;
    height: 100px;
    background: yellowgreen;
}
</style>

在这里插入图片描述

8、pinia

1、安装

pnpm i pinia

2、在src文件夹下创建store文件夹,新建index.js

//创建大仓库
import { createPinia } from 'pinia';
//createPinia方法可以用来创建大仓库
let store = createPinia();
//对外暴露,安装仓库
export default store

3、在main.js中引入使用

//引入仓库
import store from './store'
let app= createApp(App);

app.use(store).mount('#app')

4、在store文件夹下新建modules文件夹,新建info.js

1、选择式写法

1、info.js
//定义info小仓库
import { defineStore } from "pinia";

//2个参数:1、小仓库名称 2小仓库配置对象
//defineStore方法执行返回一个函数,函数作用让组件获取到仓库数据
let useInfoStore = defineStore("info",{
    //存储数据 state
    state: ()=>{
        return {
            count:99,
            arr:[1,2,3,4,5,6,7,8,9,10]
        }
    },
    actions:{
        updateCount(num){
           // this.count++
           this.count+=num;
        }
    },
    getters:{
        total(){
            return this.arr.reduce((pre,next)=>{
                return pre+next
            },0)
        }
    }
})

//对外暴露
export default useInfoStore;
2、在组件1中使用
<template>
    <div class="child">
        <h3>子组件-接收仓库数据:{{infoStore.count}}</h3>
        <h4>getters:数组求和:{{infoStore.total}}</h4>
        <button @click="updateCount">修改仓库数据</button>
    </div>
</template>

<script setup>
import useInfoStore from '../store/modules/info';
//获取小仓库对象
let infoStore = useInfoStore();
console.log(infoStore)
//修改仓库数据
let updateCount = ()=>{
    //1、直接修改
    //infoStore.count++
    //2、patch
    // infoStore.$patch({
    //     count:100
    // })
    //3、调用store方法
    infoStore.updateCount(10)
}
</script>

<style lang="scss" scoped>
.child{
    width: 300px;
    height: 200px;
    background: skyblue;
}
</style>
3、在组件2中使用
<template>
    <div class="child2">
        <h3>子组件2-接收仓库数据:{{infoStore.count}}</h3>
       
    </div>
</template>

<script setup>
import useInfoStore from '../store/modules/info';
//获取小仓库对象
let infoStore = useInfoStore();

</script>

<style lang="scss" scoped>
.child2{
    width:300px;
    height:200px;
    background: hotpink;
}
</style>

在这里插入图片描述

2、组合式API写法

1、在modules文件夹下新建todo.js
//定义组合式API
import { defineStore } from "pinia";
import { ref , computed } from 'vue'
//创建小仓库
let useTodoStore = defineStore('todo',()=>{

    //务必返回一个对象,对象的属性和方法提供给组件使用
    let todos = ref([
        {id:1,title:'吃饭'},
        {id:2,title:'睡觉'},
        {id:3,title:'打豆豆'},
    ])
    let arr=ref([1,2,3,4,5])
    const total = computed(()=>{
        return arr.value.reduce((prev, curr) => {
                return prev+curr
        }, 0)
    })
   return {
        todos,//相当于选择式API的state
        arr,
        total,
        updateTodos(){
            todos.value.push({id:4,title:'摆烂'})
        },//相当于选择式API的actions
   }

})

export default useTodoStore
2、在组件1中使用
<template>
    <div class="child">
        <h3>子组件-接收仓库数据:</h3>
        <ul>
            <li v-for="item in todosStore.todos" :key="item.id">{{item.title}}</li>
        </ul>
        <p>{{ todosStore.arr }}--总和{{todosStore.total}}</p>
        <button @click="updateTodos">修改仓库数据</button>
    </div>
</template>

<script setup>
//引入组合式API函数仓库
import useTodosStore from '../store/modules/todo';
let todosStore = useTodosStore();
//修改仓库数据
let updateTodos = ()=>{
    todosStore.updateTodos()
}
</script>

<style lang="scss" scoped>
.child{
    width: 300px;
    height: 300px;
    background: skyblue;
}
</style>
3、在组件2中使用
<template>
    <div class="child2">
        <h3>子组件2-接收仓库数据:</h3>
        <ul>
            <li v-for="item in todosStore.todos" :key="item.id">{{item.title}}</li>
        </ul>
    </div>
</template>

<script setup>
import useTodosStore from '../store/modules/todo';
//获取小仓库对象
let todosStore = useTodosStore();

</script>

<style lang="scss" scoped>
.child2{
    width:300px;
    height:300px;
    background: hotpink;
}
</style>

在这里插入图片描述

9、slot

1、默认插槽

父组件

<template>
    <div class="parent">
        <h2>父组件</h2>
        <div class="container">
            <Child>
                <pre>我是父组件传入的数据</pre>
            </Child>
        </div>
       
    </div>
</template>

<script setup>
import Child from './Child.vue';
//插槽:默认插槽、具名插槽、作用域插槽
</script>

<style lang="scss" scoped>
.parent{
    width:800px;
    height:400px;
    background: pink;
    .container{
        display: flex;
        align-items: center;
        justify-content: space-around;
    }
}
</style>

子组件

<template>
    <div class="child">
        <h3>我是子组件默认插槽</h3>
        <!-- 默认插销 -->
        <slot></slot>
        <h3>我是子组件默认插槽</h3>
       
    </div>
</template>

<script setup>
//引入组合式API函数仓库
import useTodosStore from '../store/modules/todo';
let todosStore = useTodosStore();
//修改仓库数据
let updateTodos = ()=>{
    todosStore.updateTodos()
}
</script>

<style lang="scss" scoped>
.child{
    width: 300px;
    height: 300px;
    background: skyblue;
}
</style>

在这里插入图片描述

2、具名插槽

父组件

<template>
    <div class="parent">
        <h2>父组件</h2>
        <div class="container">
            <Child>
                <!-- 具名插槽填充 v-slot:简写问# -->
                <template v-slot:a>
                    <p>我是填充具名插槽a位置结构</p>
                </template>
                <template #b>
                    <p>我是填充具名插槽b位置结构</p>
                </template>
            </Child>
        </div>
       
    </div>
</template>

<script setup>
import Child from './Child.vue';
//插槽:默认插槽、具名插槽、作用域插槽
</script>

<style lang="scss" scoped>
.parent{
    width:800px;
    height:400px;
    background: pink;
    .container{
        display: flex;
        align-items: center;
        justify-content: space-around;
    }
}
</style>

子组件

<template>
    <div class="child">
        <h3>我是子组件具名插槽</h3>
        <slot name="a"></slot>
        <h3>我是子组件具名插槽</h3>
        <hr>
        <h3>我是子组件具名插槽</h3>
        <slot name="b"></slot>
        <h3>我是子组件具名插槽</h3>
    </div>
</template>

<script setup>
//引入组合式API函数仓库
import useTodosStore from '../store/modules/todo';
let todosStore = useTodosStore();
//修改仓库数据
let updateTodos = ()=>{
    todosStore.updateTodos()
}
</script>

<style lang="scss" scoped>
.child{
    width: 300px;
    height: 300px;
    background: skyblue;
}
</style>

3、作用域插槽

父组件

<template>
    <div class="parent">
        <h2>父组件</h2>
        <div class="container">
            <Child :todos="todos">
                <template v-slot="{$row,$index}">
                   <span :style="{color:$row.done?'green':'red'}">{{ $row.title }}--下标{{$index}}</span>
                </template>
            </Child>
        </div>
       
    </div>
</template>

<script setup>
import Child from './Child.vue';
// import Child2 from './Child2.vue';
//插槽:默认插槽、具名插槽、作用域插槽
//作用域插槽:就是可以传递数据的插槽,子组件可以将数据回传给父组件,
//父组件可以决定这些回传的数据是以何种结构或者外观在子组件内部去展示
let todos = ref([
    {id:1,title:'吃饭',done:true},
    {id:2,title:'睡觉',done:false},
    {id:3,title:'打豆豆',done:true},
    {id:4,title:'打游戏',done:false},
])
</script>

<style lang="scss" scoped>
.parent{
    width:800px;
    height:400px;
    background: pink;
    .container{
        display: flex;
        align-items: center;
        justify-content: space-around;
    }
}
</style>

子组件

<template>
    <div class="child">
        <h3>子组件1</h3>
        <ul>
            <li v-for="(item,index) in todos" :key="item.id">
               <!-- 作用域插槽:可以将数据回传给父组件 -->
               <slot :$row="item" :$index="index"></slot>
            </li>
        </ul>
    </div>
</template>

<script setup>
let props = defineProps(['todos'])
</script>

<style lang="scss" scoped>
.child{
    width: 500px;
    height: 300px;
    background: skyblue;
}
</style>

在这里插入图片描述

在这里插入图片描述

### 解决PyCharm无法加载Conda虚拟环境的方法 #### 配置设置 为了使 PyCharm 能够成功识别并使用 Conda 创建的虚拟环境,需确保 Anaconda 的路径已正确添加至系统的环境变量中[^1]。这一步骤至关重要,因为只有当 Python 解释器及其关联工具被加入 PATH 后,IDE 才能顺利找到它们。 对于 Windows 用户而言,在安装 Anaconda 时,默认情况下会询问是否将它添加到系统路径里;如果当时选择了否,则现在应该手动完成此操作。具体做法是在“高级系统设置”的“环境变量”选项内编辑 `Path` 变量,追加 Anaconda 安装目录下的 Scripts 文件夹位置。 另外,建议每次新建项目前都通过命令行先激活目标 conda env: ```bash conda activate myenvname ``` 接着再启动 IDE 进入工作区,这样有助于减少兼容性方面的问题发生概率。 #### 常见错误及修复方法 ##### 错误一:未发现任何解释器 症状表现为打开 PyCharm 新建工程向导页面找不到由 Conda 构建出来的 interpreter 列表项。此时应前往 Preferences/Settings -> Project:...->Python Interpreter 下方点击齿轮图标选择 Add...按钮来指定自定义的位置。按照提示浏览定位到对应版本 python.exe 的绝对地址即可解决问题。 ##### 错误二:权限不足导致 DLL 加载失败 有时即使指定了正确的解释器路径,仍可能遇到由于缺乏适当的操作系统级许可而引发的功能缺失现象。特别是涉及到调用某些特定类型的动态链接库 (Dynamic Link Library, .dll) 时尤为明显。因此拥有管理员身份执行相关动作显得尤为重要——无论是从终端还是图形界面触发创建新 venv 流程均如此处理能够有效规避此类隐患。 ##### 错误三:网络连接异常引起依赖下载超时 部分开发者反馈过因网速慢或者其他因素造成 pip install 操作中途断开进而影响整个项目的初始化进度条卡住的情况。对此可尝试调整镜像源加速获取速度或是离线模式预先准备好所需资源包后再继续后续步骤。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值