查看“︁JavaScript/vue.js/Provide Inject”︁的源代码
来自康健生活
←
JavaScript/vue.js/Provide Inject
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 标题文本 == == ref == 配置文件 {{hc|head=conf.ts|output=<syntaxhighlight lang="javascript"> import { type InjectionKey, type Ref } from 'vue' export const DEFAULT_PARAMS = 1 export const valueKey = Symbol('value_symbol_string') as InjectionKey<Ref<number>> </syntaxhighlight>}} 父组件,上级组件 <syntaxhighlight lang="html5"> <script lang="ts" setup> import { provide, ref } from 'vue' import { DEFAULT_PARAMS, valueKey } from './config' ... const valueRef = ref(props.params || DEFAULT_PARAMS) provide(valueKey, valueRef) </script> </syntaxhighlight> 子组件 <syntaxhighlight lang="html5"> <script lang="ts" setup> import { inject, ref } from 'vue' import { DEFAULT_PARAMS, valueKey } from './config' ... const value= inject(valueKey, ref(DEFAULT_PARAMS)) </syntaxhighlight> === reactive === 配置文件 {{hc|head=conf.ts|output=<syntaxhighlight lang="javascript"> import { type InjectionKey, type Reactive } from 'vue' export interface IFRect { x: number y: number width: number height: number } export const DEFAULT_RECT = { x: 0, y: 0, width: 0, height: 0, } export const rectKey: InjectionKey<Reactive<IFRect>> = Symbol('rect') </script> </syntaxhighlight>}} 父组件,上级组件 <syntaxhighlight lang="html5"> <script lang="ts" setup> import { provide, reactive, type PropType } from 'vue' import { DEFAULT_RECT, rectKey, type IFRect } from './config' ... const props = defineProps({ rect: { type: Object as PropType<IFRect>, default: () => DEFAULT_RECT as IFRect, }, }) const rectReactive = reactive(props.rect || DEFAULT_RECT) provide(rectKey, rectReactive) </script> </syntaxhighlight> 子组件 <syntaxhighlight lang="html5"> <script lang="ts" setup> import { inject, reactive } from 'vue' import { DEFAULT_RECT, rectKey} from './config' ... const rect = inject(rectKey, reactive(DEFAULT_RECT)) </script> </syntaxhighlight>
此页面嵌入的页面:
模板:Hc
(
查看源代码
)
模板:Hc/style.css
(
查看源代码
)
模板:Unknown
(
查看源代码
)
返回
JavaScript/vue.js/Provide Inject
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。