JavaScript/vue.js/Provide Inject:修订间差异
来自康健生活
| (未显示同一用户的5个中间版本) | |||
| 第1行: | 第1行: | ||
== | == 默认值 == | ||
在 TS 中使用 Inject 时经常会遇到 undefined 的问题,这个时候就需要使用一个默认值来进行消除。 | |||
<syntaxhighlight lang="javascript"> | |||
// eq: | |||
inject(curKey, ref('$'))</syntaxhighlight> | |||
== ref == | == ref == | ||
配置文件 | 配置文件 | ||
| 第28行: | 第34行: | ||
... | ... | ||
const value= inject(valueKey, ref(DEFAULT_PARAMS)) | const value= inject(valueKey, ref(DEFAULT_PARAMS)) | ||
</script> | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| 第47行: | 第54行: | ||
} | } | ||
export const rectKey: InjectionKey<Reactive<IFRect>> = Symbol('rect') | export const rectKey: InjectionKey<Reactive<IFRect>> = Symbol('rect') | ||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
| 第73行: | 第79行: | ||
<script lang="ts" setup> | <script lang="ts" setup> | ||
import { inject, reactive } from 'vue' | import { inject, reactive } from 'vue' | ||
import { DEFAULT_RECT, rectKey} from './config' | import { DEFAULT_RECT, rectKey } from './config' | ||
... | ... | ||
const rect = inject(rectKey, reactive(DEFAULT_RECT)) | const rect = inject(rectKey, reactive(DEFAULT_RECT)) | ||
</script> | </script> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== function === | |||
配置文件 | |||
{{hc|head=conf.ts|output=<syntaxhighlight lang="javascript"> | |||
import { type InjectionKey, type Reactive } from 'vue' | |||
export interface IFLineList { | |||
horizontal: number[] | |||
vertical: number[] | |||
} | |||
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 lang="html5"> | |||
<script lang="ts" setup> | |||
import { provide, reactive } from 'vue' | |||
import { lineListKey, DEFAULT_LINELIST } from './config' | |||
... | |||
const lineList = reactive<IFLineList>(DEFAULT_LINELIST.lineList) | |||
const updateLineList = () => {} | |||
provide(lineListKey, { | |||
lineList, | |||
updateLineList, | |||
}) | |||
</script> | |||
</syntaxhighlight> | |||
子组件 | |||
<syntaxhighlight lang="html5"> | |||
<script lang="ts" setup> | |||
import { inject, reactive } from 'vue' | |||
import { lineListKey, DEFAULT_LINELIST } from './config' | |||
... | |||
const { lineList, updateLineList } = inject(lineListKey, DEFAULT_LINELIST) | |||
</script> | |||
</syntaxhighlight> | |||
== 扩展 == | |||
在网上看到一种扩展的思路,留存以做思考。 | |||
<syntaxhighlight lang="javascript"> | |||
// in utils.ts | |||
function injectStrict<T>(key: InjectionKey<T>, fallback?: T) { | |||
const resolved = inject(key, fallback); | |||
if (!resolved) { | |||
throw new Error(`Could not resolve ${key.description}`); | |||
} | |||
return resolved; | |||
} | |||
// Another vue file | |||
import { injectStrict } from './utils.ts'; | |||
const product = injectStrict(ProductKey); | |||
product.value; </syntaxhighlight>{{Unknown}} | |||