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

vue3 provide inject使用

时间:2024-06-23 22:42:57浏览次数:3  
标签:vue console log provide provideData2 inject vue3 ref

概论

provide 就是父类用来提供数据给子类或者孙子类
image

inject 就是子类或者孙子类用来获取父类或者祖先提供的provide数据
image

代码

  • app.vue 祖先层
<template>
  <header>
    <img alt="Vue logo" class="logo" src="@/assets/logo.svg" width="125" height="125" />

    <div class="wrapper">
      <HelloWorld ref="HelloWorldRef" msg="You did it!" />
      <div>
        app_provideData2:{{provideData2}}
      </div>
      <nav>
        <RouterLink to="/">Home</RouterLink>
        <RouterLink to="/about">About</RouterLink>
      </nav>
    </div>
  </header>

  <RouterView />
</template>


<script lang="tsx" setup>
import { ref, provide } from "vue"
import { RouterLink, RouterView } from 'vue-router'
import HelloWorld from './components/HelloWorld.vue'
import "./components/myts"

const HelloWorldRef = ref('my_injected_value')

console.log(HelloWorldRef, "HelloWorldRef")

provide('HelloWorldRef', HelloWorldRef.value)


const provideData2=ref<string>("i") //把ref注入到provide,孙子类都可以修改到这个数据

provide('provideData2',provideData2)

</script>


<style scoped>
header {
  line-height: 1.5;
  max-height: 100vh;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
}

nav {
  width: 100%;
  font-size: 12px;
  text-align: center;
  margin-top: 2rem;
}

nav a.router-link-exact-active {
  color: var(--color-text);
}

nav a.router-link-exact-active:hover {
  background-color: transparent;
}

nav a {
  display: inline-block;
  padding: 0 1rem;
  border-left: 1px solid var(--color-border);
}

nav a:first-of-type {
  border: 0;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  .logo {
    margin: 0 2rem 0 0;
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }

  nav {
    text-align: left;
    margin-left: -1rem;
    font-size: 1rem;

    padding: 1rem 0;
    margin-top: 1rem;
  }
}
</style>

  • helloworld.vue 子类
<template>
  <div class="greetings">
    <h1 class="green">{{ msg }}</h1>
    <h3>
      You’ve successfully created a project with
      <a href="https://vitejs.dev/" target="_blank" rel="noopener">Vite</a> +
      <a href="https://vuejs.org/" target="_blank" rel="noopener">Vue 3</a>.
    </h3>
    <div>{{ abd }}</div>
    <div>fatherInject:{{ fatherInject }}</div>
    <hChild />
  </div>
</template>


<script lang="ts" setup>
import { reactive, ref, inject } from "vue"
import hChild from "./hChild.vue"



const fatherInject = inject("HelloWorldRef")

console.log(fatherInject, "fatherInject")





defineProps({
  msg: {
    type: String,
    required: true
  }
})

let abc = ref<string>("abc")

console.log(abc.value, "value")

let abd = ref("abd")


console.log(abd, "abd")

let abf = reactive({
  a: {
    b: {
      c: {
        d: 'sklfjslkfjslkf'
      }
    }
  }
})

console.log(abf, "abf")

let abu = ref({
  a: {
    b: {
      c: {
        d: 'sklfjslkfjslkf'
      }
    }
  }
})
console.log(abu, "abu")

defineExpose({
  abc: abc
})



// 定义一个普通的对象obj
const obj = {
  name: "_island"
};

// 代理obj这个对象,并传入get捕获器
const objProxy = new Proxy(obj, {
  // get捕获器
  get: function (target, key) {
    console.log(`捕获到对象获取${key}属性的值操作`);
    return target[key];
  },
});

// 通过代理对象操作obj对象
console.log(objProxy.name);

console.log(objProxy, "objproxy")
// 捕获到对象获取name属性的值操作
// _island

interface myabc {
  one: string,
  two: number,
  three: string,
}


interface myabf extends Omit<myabc, 'one' | 'two'> { }

let abe: myabf = {
  three: "skfjlsfjsldf"
}

console.log(abe, "abe")


</script>

<style scoped>
h1 {
  font-weight: 500;
  font-size: 2.6rem;
  position: relative;
  top: -10px;
}

h3 {
  font-size: 1.2rem;
}

.greetings h1,
.greetings h3 {
  text-align: center;
}

@media (min-width: 1024px) {

  .greetings h1,
  .greetings h3 {
    text-align: left;
  }
}
</style>
  • hchild.vue 孙子类
<template>
  <div>
    <div>getInject:{{ injected_from_root }}</div>

    <div>provideData2:{{ provideData2 }}</div>
  </div>
</template>


<script lang="ts" setup>
import { reactive, ref, inject, getCurrentInstance, Ref } from "vue"

const { ctx } = getCurrentInstance()

console.log(ctx, "ctx-child-child") //这里ctx等同vue2 this 
const injected_from_root = inject<string>("HelloWorldRef") //这里可以获取到祖先辈的provide数据,说明provide可以多层传递到孙子级别等等
console.log(injected_from_root, "injected_from_root-child-child")

console.log(this, "this-child-child") // undefined this 打印不了,说明默认没this


const provideData2 = inject<Ref>("provideData2"); //ref 对象

console.log(provideData2, "provideData2-child-child") //传递过来时一个ref对象
setTimeout(() => {
  provideData2.value = "hchild_fix" //可以对ref的value进行修改,从而可以修改到祖先的数据
}, 2000)
</script>

<style scoped></style>

标签:vue,console,log,provide,provideData2,inject,vue3,ref
From: https://www.cnblogs.com/jocongmin/p/18264049

相关文章

  • Vue3中watch怎么解决静态问题的
    在Vue3中,`watch`函数用于观察和响应Vue响应式系统中的数据变化。Vue3的响应式系统基于Proxy,这使得它能够更细粒度地追踪依赖和更新DOM。然而,在使用`watch`时,有时可能会遇到所谓的“静态问题”,这通常是指`watch`函数内部引用的静态数据或计算属性在组件的整个生命......
  • antdesign-vue3 List的分页器最全配置
    AntDesignVue官网:https://www.antdv.com/components/list-cn何时使用#最基础的列表展示,可承载文字、列表、图片、段落,常用于后台数据展示页面。<a-listsize="large"bordered:data-source="listData":pagination="pagination"><......
  • 一、若依--P2--P5【黑马程序员Java最新AI+若依框架项目开发新方案视频教程,基于RuoYi-V
    学习视频【黑马程序员Java最新AI+若依框架项目开发新方案视频教程,基于RuoYi-Vue3前后端分离版本,从前端到后端再到AI智能化应用全通关】https://www.bilibili.com/video/BV1pf421B71v/?p=6&share_source=copy_web&vd_source=3949d51b57b2891ea14d6e51c792bef6P2:前端框架搭......
  • 【区分vue2和vue3下的element UI Descriptions 描述列表组件,分别详细介绍属性,事件,方法
    在ElementUI(为Vue2设计)和ElementPlus(为Vue3设计)中,Descriptions(描述列表)组件通常用于展示一系列的结构化信息。然而,需要明确的是,ElementUI官方库中并没有直接名为Descriptions的组件,但在ElementPlus中,该组件是存在的。以下将分别介绍ElementPlus中的De......
  • 【区分vue2和vue3下的element UI Result 结果组件,分别详细介绍属性,事件,方法如何使用,并
    在Vue2中,ElementUI并没有直接提供名为Result的组件。但是,在Vue3的ElementPlus中,Result组件是用来展示操作结果或状态信息的。以下是ElementPlus中Result组件的详细介绍,以及如何在Vue3中使用它。由于Vue2没有该组件,我将只介绍Vue3下的使用。Vu......
  • Vue3的Composition API:Composition API是Vue3的一大新特性,它提供了一种更灵活的方式来
    1.介绍1.什么是CompositionAPI CompositionAPI是Vue.js3.0中引入的一项新特性,提供了一种新的、更灵活的方式来组织Vue组件的代码。CompositionAPI是基于函数的,并允许在组件的setup函数中直接处理响应式数据和生命周期钩子,使得代码更加清晰,更便于维护和测......
  • Vite-Wechat网页聊天室|vite5.x+vue3+pinia+element-plus仿微信客户端
    基于Vue3+Pinia+ElementPlus仿微信网页聊天模板Vite5-Vue3-Wechat。vite-wechat使用最新前端技术vite5+vue3+vue-router@4+pinia+element-plus搭建网页端仿微信界面聊天系统。包含了聊天、通讯录、朋友圈、短视频、我的等功能模块。支持收缩侧边栏、背景壁纸换肤、锁屏、最大化等......
  • 对比Vue2/Vue3项目如何自定义插件
    学习目标:对比Vue2/Vue3项目如何自定义插件学习内容:插件(Plugins)是一种能为Vue添加全局功能的工具代码。一个插件可以是一个拥有 install() 方法的对象,也可以直接是一个安装函数本身。安装函数会接收到安装它的应用实例传递给Vue.use()/ app.use() 的额外选项作......
  • Vue30_Vuejs ajax5
    Vuejs并没有直接处理ajax的组件,但可以使用axios或vue-resource组件实现对异步请求的操作。 一、vue-resourcevue-resource是Vue.js的插件提供了使用XMLHttpRequest或JSONP进行Web请求和处理响应的服务。当vue更新到2.0之后,作者就宣告不再对vue-resource更新,而是推荐axios......
  • vue3实现模拟地图上,站点名称按需显示的功能
    很久很久没有更新博客了,因为实在是太忙了,每天都有公司的事情忙不完.......最近在做车辆模拟地图,在实现控制站点名称按需显示时,折腾了好一段时间,特此记录一下。最终界面如下图所示:站点显示需求:首末站必须显示,从第一个站开始,如果站点名称能显示下,则显示,如果站点名称会重叠则隐藏,......