Vue 全组件 局部组件

一、组件定义和使用

1、全局组件

定义

<template>

<div>

<h1>This is a global component</h1>

</div>

</template>

<script lang="ts">

</script>

<style></style>

导入

全局组件在main.ts(Vue + TS的项目)引入,

或者在main.js(Vue + JS的项目)引入

import { createApp } from 'vue'

import App from './App.vue'

import router from './router'

import store from './store'

import GlobalComponent from "../src/components/component/GlobalComponent.vue";

const app = createApp(App)

app.component("GlobalComponent",GlobalComponent);

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

使用

全局组件可以在任意组件中使用,不需要再次导入

2、局部组件

定义

导入与使用

二、组件通信

1、props

(1)传递单个值

(2)传递多个值

如果传递的时对象,对象在被子组件的props接收时需要解构,依次写在props数组中

传递的如果是一个个的值,则直接在props数组中写入即可

2、插槽

插槽的作用:让子组件可以接收父组件传过来的元素、组件

(1)匿名插槽

如果父元素只传递一个元素,或者传递的多个元素会在一个地方使用,那么可以使用匿名插槽

(2)具名插槽

父组件会传递多个元素,这些元素需要再不同的地方使用,这时需要使用具名插槽进行区分

(3)作用域插槽

父组件需要用到子组件中的数据时,可以使用作用域插槽将子组件的数据传递过去

子组件

<template>

<div>

<h1>This is a local component</h1>

<slot v-for="(item,index) in list" :item="item" :index="index"/>

</div>

</template>

<script lang="ts" setup>

const list = [1,2,3,4];

</script>
//父组件

<template>

<div>

<h1>This is a component view</h1>

<div>

<GlobalComponent />

<LocalComponent v-slot="slotProps">

<div>{{ slotProps.item }}*****{{ slotProps.index }}</div>

</LocalComponent>

</div>

</div>

</template>
//父组件写法二

<template>

<div>

<h1>This is a component view</h1>

<div>

<GlobalComponent />

<LocalComponent v-slot="{item,index}">

<div>{{ item }}*****{{ index }}</div>

</LocalComponent>

</div>

</div>

</template>

3、provide&inject

父组件给子组件传值,子组件使用props接收,如果孙子组件乃至重孙子组件要使用父组件的数据,那么就要一层层用props传递,特别麻烦。

父组件使用provide(提供)提供数据

子组件、孙子组件、重孙子组件....可以使用inject接收数据

//父组件

<template>

<div>

<LocalComponent />

</div>

</template>

<script setup lang="ts">

import LocalComponent from "../components/component/LocalComponent.vue";

import { provide, ref } from "vue";

const msg = ref('hello world');

provide('msg',msg);

</script>
//子组件

<template>

<div>

LocalComponent

<GrandChild />

</div>

</template>

<script lang="ts" setup>

import GrandChild from '../component/GrandChild.vue';

</script>
//孙子组件

<template>

<div>

<h1>This is a GrandChild component</h1>

{{ msg }}

</div>

</template>

<script lang="ts" setup>

import { inject } from "vue";

const msg = inject('msg');

</script>

<style></style>

4、事件通信

三、异步组件

相关推荐
zl0_00_016 分钟前
xss-lab
前端·网络·xss
爱上你家菜包23 分钟前
Electron一小时快速上手
前端·javascript·electron
陈琦鹏24 分钟前
Vue3+Vite开发Electron桌面端问题记录
前端·javascript·electron
什么什么什么?1 小时前
el-input实现金额输入
javascript·vue.js·elementui
工业互联网专业2 小时前
基于springboot+vue的融合多源高校画像数据与协同过滤算法的高考择校推荐系统
java·vue.js·spring boot·毕业设计·源码·课程设计·高考择校推荐系统
狼性书生2 小时前
uniapp vue3实现的一款数字动画调节器件,支持长按、单点操作,提供丝滑的增减动画效果
前端·vue.js·微信小程序·小程序·uni-app
Jelena157795857922 小时前
爬虫获取微店商品快递费 item_feeAPI 接口的完整指南
开发语言·前端·爬虫
qq_12498707532 小时前
Java+Vue+uniapp微信小程序校园自助打印系统(程序+论文+讲解+安装+调试+售后)
vue.js·微信小程序·uni-app·毕业设计
总是学不会.2 小时前
从“记住我”到 Web 认证:Cookie、JWT 和 Session 的故事
java·前端·后端·开发
xinran_Yi3 小时前
XSS-labs靶场通关
前端·xss