ઓપ્શન્સ: કમ્પોઝિશન (Options: Composition)
provide
વંશજ (descendant) ઘટકો દ્વારા inject કરી શકાય તેવી values provide કરો.
ટાઇપ (Type)
tsinterface ComponentOptions { provide?: object | ((this: ComponentPublicInstance) => object) }વિગત (Details)
provideઅનેinjectએકસાથે ઉપયોગ થાય છે જેથી પૂર્વજ (ancestor) ઘટક તેના તમામ descendants માટે dependency injector તરીકે કાર્ય કરી શકે, ભલે component hierarchy કેટલી ઊંડી (deep) હોય, જ્યાં સુધી તેઓ સમાન parent chain માં હોય.provideઓપ્શન ક્યાં તો object અથવા object return કરતું function હોવું જોઈએ. આ object તેના descendants માં injection માટે ઉપલબ્ધ properties ધરાવે છે. આ object માં keys તરીકે Symbols ઉપયોગ કરી શકો.ઉદાહરણ (Example)
મૂળભૂત ઉપયોગ:
jsconst s = Symbol() export default { provide: { foo: 'foo', [s]: 'bar' } }Per-component state provide કરવા function ઉપયોગ કરવું:
jsexport default { data() { return { msg: 'foo' } } provide() { return { msg: this.msg } } }નોંધ કરો કે ઉપરના ઉદાહરણમાં, provide કરેલું
msgreactive નહીં હોય. વધુ વિગત માટે Reactivity સાથે કામ કરવું જુઓ.આ પણ જુઓ Provide / Inject
inject
પૂર્વજ (ancestor) providers માંથી શોધીને વર્તમાન ઘટકમાં inject કરવાની properties declare કરો.
ટાઇપ (Type)
tsinterface ComponentOptions { inject?: ArrayInjectOptions | ObjectInjectOptions } type ArrayInjectOptions = string[] type ObjectInjectOptions = { [key: string | symbol]: | string | symbol | { from?: string | symbol; default?: any } }વિગત (Details)
injectઓપ્શન ક્યાં તો હોવું જોઈએ:- Strings ની array, અથવા
- Object જ્યાં keys local binding name છે અને value ક્યાં તો:
- ઉપલબ્ધ injections માં શોધવા માટેની key (string અથવા Symbol), અથવા
- Object જ્યાં:
fromproperty ઉપલબ્ધ injections માં શોધવા માટેની key (string અથવા Symbol) છે, અનેdefaultproperty fallback value તરીકે ઉપયોગ થાય. Props default values ની જેમ, multiple component instances વચ્ચે value sharing ટાળવા object types માટે factory function જરૂરી છે.
Injected property
undefinedહશે જો matching property કે default value provide ન કરવામાં આવ્યા હોય.નોંધ કરો કે injected bindings reactive નથી. આ ઇરાદાપૂર્વક છે. જો કે, injected value reactive object હોય, તો તે object ની properties reactive રહે છે. વધુ વિગત માટે Reactivity સાથે કામ કરવું જુઓ.
ઉદાહરણ (Example)
મૂળભૂત ઉપયોગ:
jsexport default { inject: ['foo'], created() { console.log(this.foo) } }Injected value ને prop માટે default તરીકે ઉપયોગ કરવી:
jsconst Child = { inject: ['foo'], props: { bar: { default() { return this.foo } } } }Injected value ને data entry તરીકે ઉપયોગ કરવી:
jsconst Child = { inject: ['foo'], data() { return { bar: this.foo } } }Injections default value સાથે optional હોઈ શકે:
jsconst Child = { inject: { foo: { default: 'foo' } } }જો અલગ name ની property માંથી inject કરવાની જરૂર હોય, તો source property દર્શાવવા
fromઉપયોગ કરો:jsconst Child = { inject: { foo: { from: 'bar', default: 'foo' } } }Prop defaults ની જેમ, non-primitive values માટે factory function ઉપયોગ કરવું જરૂરી:
jsconst Child = { inject: { foo: { from: 'bar', default: () => [1, 2, 3] } } }આ પણ જુઓ Provide / Inject
mixins
વર્તમાન ઘટકમાં mix કરવા માટે option objects ની array.
ટાઇપ (Type)
tsinterface ComponentOptions { mixins?: ComponentOptions[] }વિગત (Details)
mixinsઓપ્શન mixin objects ની array accept કરે છે. આ mixin objects normal instance objects ની જેમ instance options ધરાવી શકે, અને ચોક્કસ option merging logic ઉપયોગ કરીને eventual options સાથે merge થશે. ઉદાહરણ તરીકે, જો તમારા mixin માંcreatedhook હોય અને ઘટક પોતે પણ ધરાવે, તો બંને functions call થશે.Mixin hooks જે ક્રમમાં provide કરવામાં આવે છે તે ક્રમમાં call થાય છે, અને ઘટકના પોતાના hooks પહેલા call થાય છે.
હવે ભલામણ નથી (No Longer Recommended)
Vue 2 માં, mixins ઘટક logic ના reusable chunks બનાવવાની primary mechanism હતી. Vue 3 માં mixins supported રહે છે, પરંતુ Composition API ઉપયોગ કરતા Composable functions હવે ઘટકો વચ્ચે code reuse માટે preferred approach છે.
ઉદાહરણ (Example)
jsconst mixin = { created() { console.log(1) } } createApp({ created() { console.log(2) }, mixins: [mixin] }) // => 1 // => 2
extends
Extend કરવા માટે "base class" ઘટક.
ટાઇપ (Type)
tsinterface ComponentOptions { extends?: ComponentOptions }વિગત (Details)
એક ઘટકને અન્ય extend કરવાની, તેના component options inherit કરવાની મંજૂરી આપે છે.
Implementation perspective થી,
extendsmixinsની લગભગ identical છે.extendsદ્વારા specified ઘટકને એવી રીતે treat કરવામાં આવશે જાણે તે પ્રથમ mixin હોય.જો કે,
extendsઅનેmixinsઅલગ intents express કરે છે.mixinsઓપ્શનનો ઉપયોગ મુખ્યત્વે functionality ના chunks compose કરવા માટે થાય, જ્યારેextendsમુખ્યત્વે inheritance સાથે concerned છે.mixinsની જેમ, કોઈપણ options (setup()સિવાય) relevant merge strategy ઉપયોગ કરીને merge થશે.ઉદાહરણ (Example)
jsconst CompA = { ... } const CompB = { extends: CompA, ... }Composition API માટે ભલામણ નથી
extendsOptions API માટે ડિઝાઇન થયેલું છે અનેsetup()hook ના merging ને handle કરતું નથી.Composition API માં, logic reuse માટે preferred mental model "inheritance" કરતા "compose" છે. જો તમારી પાસે ઘટકમાંથી logic છે જે બીજામાં reuse કરવાની જરૂર છે, તો relevant logic ને Composable માં extract કરવાનું વિચારો.
જો તમે હજુ Composition API ઉપયોગ કરીને ઘટકને "extend" કરવા ઇચ્છો, તો extending ઘટકના
setup()માં base ઘટકનાsetup()ને call કરી શકો:jsimport Base from './Base.js' export default { extends: Base, setup(props, ctx) { return { ...Base.setup(props, ctx), // local bindings } } }