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' ... co…” |
|||
| (未显示同一用户的8个中间版本) | |||
| 第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> | ||
| 第33行: | 第40行: | ||
配置文件 | 配置文件 | ||
{{hc|head=conf.ts|output=<syntaxhighlight lang="javascript"> | {{hc|head=conf.ts|output=<syntaxhighlight lang="javascript"> | ||
import { type InjectionKey, type | import { type InjectionKey, type Reactive } from 'vue' | ||
export const | export interface IFRect { | ||
export const | 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') | |||
</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> | |||
=== 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>}} | ||
| 第42行: | 第111行: | ||
<syntaxhighlight lang="html5"> | <syntaxhighlight lang="html5"> | ||
<script lang="ts" setup> | <script lang="ts" setup> | ||
import { provide, | import { provide, reactive } from 'vue' | ||
import { | import { lineListKey, DEFAULT_LINELIST } from './config' | ||
... | ... | ||
const | const lineList = reactive<IFLineList>(DEFAULT_LINELIST.lineList) | ||
provide( | const updateLineList = () => {} | ||
provide(lineListKey, { | |||
lineList, | |||
updateLineList, | |||
}) | |||
</script> | </script> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| 第54行: | 第127行: | ||
<syntaxhighlight lang="html5"> | <syntaxhighlight lang="html5"> | ||
<script lang="ts" setup> | <script lang="ts" setup> | ||
import { inject, | import { inject, reactive } from 'vue' | ||
import { | import { lineListKey, DEFAULT_LINELIST } from './config' | ||
... | ... | ||
const | const { lineList, updateLineList } = inject(lineListKey, DEFAULT_LINELIST) | ||
</script> | |||
</syntaxhighlight> | </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}} | |||