首页 > 其他分享 >vue3中使用provide/inject

vue3中使用provide/inject

时间:2023-08-28 09:11:25浏览次数:44  
标签:vue const provide messages inject vue3 import

父组件

<template>
  <hello-world/>
  <button @click="changeMessage">按钮</button>
</template>
<script setup lang="ts">
import HelloWorld from "./components/HelloWorld.vue";
import {provide, ref} from "vue";

const messages = ref("消息提示")
provide("messages",messages)
function changeMessage() {
  messages.value = "改变消息" +  Math.random()
  console.log(messages.value)
}


</script>
<style scoped lang="scss">

</style>

子组件

<script setup lang="ts">
import {inject, ref} from 'vue'
import LittlePage from "./little-page.vue";
const messages = inject("messages")
defineProps<{ msg: string }>()

const count = ref(0)
</script>

<template>
  {{messages}}
  <little-page/>
</template>

<style scoped>
.read-the-docs {
  color: #888;
}
</style>

孙组件

<template>
<h1>{{message}}</h1>
</template>

<script setup lang="ts">
import {inject} from "vue";

const message = inject("messages")
</script>

<style scoped lang="less">

</style>

 

标签:vue,const,provide,messages,inject,vue3,import
From: https://www.cnblogs.com/hhcome/p/17661377.html

相关文章

  • vue3探索——组件通信之v-model父子组件数据同步
    背景再很多场景中,我们可能想在子组件中修改父组件的数据,但事实上,vue不推荐我们这么做,因为数据的修改不容易溯源。Vue2写法在vue2中,我们使用.sync修饰符+自定义事件'update:xxx',来使父子组件数据同步。//父组件<template><div><h2>我是父组件,我有{{money}}¥......
  • Vue3.3 + TS4 ,自主打造媲美 ElementPlus的组件库(16章)
    点击下载:Vue3.3+TS4,自主打造媲美ElementPlus的组件库(16章)提取码:n899 Vue3.3+TS4,自主打造媲美ElementPlus的组件库课程,又名:进阶必学,2023最新,打造媲美ElementPlus的组件库随着Web应用程序的复杂性不断增加,为了提高开发效率和维护性,我们往往需要构建可复用的组件库。Vue作为......
  • vue3中组件,api的自动导入
    vue3中ref,reactive等api和自定义组件等每个页面都要引入很麻烦,偷懒是人的天性,故引入自动导入插件,以此记录:1.vue3的api自动导入使用前:<scriptsetuplang="ts">import{ref,onMounted}from'vue'constimgUrl=ref("")constcanvas=ref()onMounted(()=>......
  • 在Vue3中使用 Pinia 保姆教程
    前言Vue3是Vue3是一款非常流行的JavaScript框架,它的出现为我们开发Web应用程序提供了更加高效和便捷的方式。在Vue3中,我们可以使用Pinia来进行状态管理,这是一个非常流行的状态管理库,它可以帮助我们更好地组织和管理应用程序的状态。在本文中,我们将探讨如何使用Vue3和Pinia,并持久化......
  • vue3 监听容器滚动到底部
    在容器里面添加@scroll事件<template><div@scroll="scrolling"id="content"><pv-for="iinArray.from({length:30},(v,i)=>i)">{{i}}</p></div><divv-if="bottom"......
  • Vue3内置组件suspense用法
    1、作用在使用异步组件时,由于需要等待组件加载完成后才能显示,因此可能会出现页面空白或显示错误信息的情况。而Suspense组件的作用就是在异步组件加载完成前显示一个占位符,提高用户体验。2、用法首先子组件AsyncShow:使用了promise包裹代码<template><div><h1>{{re......
  • 关于C#里IFormatProvider与IFormattable的一些思考
    一,从时间(DateTime)出发先上一段处理时间格式化的代码。该代码在Net6.0框架下运行。vartime=newDateTime(2023,8,24);Console.WriteLine(time);Console.WriteLine(string.Format("{0:yyyyMMdd}",time));//写法1......
  • vue3 使用 setup 语法糖时,keep-alive 缓存使用 include / exclude 获取组件名
    <template><router-viewv-slot="{Component,route}"><keep-alive:include="['ComponentName']"><component:is="Component":key="route.name"/></keep-alive>......
  • vue3 报错:husky - pre-commit hook exited with code 1 (error)
    问题:git提交不上去解决方法:   "format":"prettier--write\"./**/*.{html,vue,ts,js,json,md}\"",......
  • Vue3 中 keepAlive 如何搭配 VueRouter 来更自由的控制页面的状态缓存?
    在vue中,默认情况下,一个组件实例在被替换掉后会被销毁。这会导致它丢失其中所有已变化的状态——当这个组件再一次被显示时,会创建一个只带有初始状态的新实例。但是vue提供了keep-alive组件,它可以将一个动态组件包装起来从而实现组件切换时候保留其状态。本篇文章要介绍的......