JavaScript/vue.js/Provide Inject:修订间差异
来自康健生活
| 第83行: | 第83行: | ||
{{hc|head=conf.ts|output=<syntaxhighlight lang="javascript"> | {{hc|head=conf.ts|output=<syntaxhighlight lang="javascript"> | ||
import { type InjectionKey, type Reactive } from 'vue' | import { type InjectionKey, type Reactive } from 'vue' | ||
export interface | export interface IFLineList { | ||
horizontal: number[] | |||
vertical: number[] | |||
} | } | ||
export const | export interface IFLineListProvide { | ||
lineList: Reactive<IFLineList> | |||
updateLineList: Function | |||
} | |||
export const lineListKey: InjectionKey<IFLineListProvide> = Symbol('lineList') | |||
export const DEFAULT_LINELIST = { | |||
lineList: { | |||
horizontal: [] as number[], | |||
vertical: [] as number[], | |||
}, | |||
updateLineList: () => {}, | |||
} | } | ||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
| 第102行: | 第105行: | ||
<syntaxhighlight lang="html5"> | <syntaxhighlight lang="html5"> | ||
<script lang="ts" setup> | <script lang="ts" setup> | ||
import { provide, reactive | import { provide, reactive } from 'vue' | ||
import { | import { lineListKey, DEFAULT_LINELIST } from './config' | ||
... | ... | ||
const | const lineList = reactive<IFLineList>(DEFAULT_LINELIST.lineList) | ||
const updateLineList = () => {} | |||
provide(lineListKey, { | |||
lineList, | |||
updateLineList, | |||
}) | }) | ||
</script> | </script> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| 第121行: | 第122行: | ||
<script lang="ts" setup> | <script lang="ts" setup> | ||
import { inject, reactive } from 'vue' | import { inject, reactive } from 'vue' | ||
import { | import { lineListKey, DEFAULT_LINELIST } from './config' | ||
... | ... | ||
const | const { lineList, updateLineList } = inject(lineListKey, DEFAULT_LINELIST) | ||
</script> | </script> | ||
</syntaxhighlight> | </syntaxhighlight> | ||