日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

vue3中 inject provide的响应式使用

發布時間:2024/3/13 vue 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vue3中 inject provide的响应式使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

祖組件

<template><div class="text">測試 piano或者inject測試index{{ menuVisible }}<Parent /></div> </template><script> import {defineComponent,getCurrentInstance,reactive,provide,ref, } from "vue"; import Parent from "@/views/ceShi/modules/parent.vue";export default defineComponent({name: "",// 注冊你的組件components: { Parent },emits: {},setup(props, { attrs, slots, emit, expose }) {const menuVisible = ref(true);provide("menuVisible", menuVisible);return {menuVisible,attrs, // Attribute (非響應式對象,等同于 attrs),有狀態,會隨組件本身的更新而更新slots,emit, // 觸發事件 (方法,等同于 emit)expose, // 暴露公共 property (函數)};},methods: {}, }); </script><style lang="scss" scoped> .text {color: #ccc; } </style>

父組件

<template><div class="parent">parent{{ menuVisible }}<Children /><!-- <button @click="toggle">點擊父組件切換</button> --></div> </template><script> import {defineComponent,getCurrentInstance,reactive,ref,inject, } from "vue"; import Children from "@/views/ceShi/modules/children.vue";export default defineComponent({name: "",// 注冊你的組件components: { Children },props: {},emits: {},setup(props, { attrs, slots, emit, expose }) {const menuVisible = inject("menuVisible");return {menuVisible,attrs, // Attribute (非響應式對象,等同于 attrs),有狀態,會隨組件本身的更新而更新slots,emit, // 觸發事件 (方法,等同于 emit)expose, // 暴露公共 property (函數)};},methods: {}, }); </script><style lang="scss" scoped></style>

孫子組件

<template><div class="children">children{{ menuVisibleChildren }}<!-- {{ menuVisible }} --><button @click="togal">點擊切換</button></div> </template><script> import {defineComponent,getCurrentInstance,reactive,ref,inject, } from "vue";export default defineComponent({name: "",// 注冊你的組件components: {},props: {},emits: {},setup(props, { attrs, slots, emit, expose }) {const menuVisibleChildren = inject("menuVisible");const togal = () => {menuVisibleChildren.value = !menuVisibleChildren.value;};return {togal,menuVisibleChildren,attrs, // Attribute (非響應式對象,等同于 attrs),有狀態,會隨組件本身的更新而更新slots,emit, // 觸發事件 (方法,等同于 emit)expose, // 暴露公共 property (函數)};},methods: {}, }); </script><style lang="scss" scoped></style>

總結

以上是生活随笔為你收集整理的vue3中 inject provide的响应式使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。