Heim  >  Fragen und Antworten  >  Hauptteil

Geben Sie die Bereitstellungs-/Injektionsfunktion für Referenzen in VueJS ein

<p>Ich versuche, einen typisierten Verweis aus einer übergeordneten Komponente bereitzustellen und ihn in die untergeordnete Komponente einzufügen. </p> <p>Ich bin nicht sicher, ob ich die VueJS-Dokumentation verstehe, aber das ist es, was ich mir ausgedacht habe. </p> <pre class="brush:php;toolbar:false;">import type { Ref } from 'vue' Importtyp {InjectionKey} aus 'vue' Importieren Sie den Typ DocumentSet aus „@/types/DocumentSet“. const documentSet = Symbol() as InjectionKey<{ documentSet: Ref<DocumentSet>, aktualisieren: () => }> Standarddokumentsatz</pre> exportieren <p>Die Aufgabe besteht also darin, ein typisiertes Objekt (in meinem Fall ein documentSet) und eine Funktion bereitzustellen, die es aktualisiert. </p> <p>Jetzt habe ich in der übergeordneten Komponente diesen Code: </p> <pre class="brush:php;toolbar:false;">dsKey aus '@/injectionKeys/documentSet' importieren Importieren Sie den Typ DocumentSet aus „@/types/DocumentSet“. ... const documentSet = ref<DocumentSet |. Provide(dsKey, { documentSet, update: () => console.log('update') })</pre> <p>Und in der untergeordneten Komponente habe ich diesen Code: </p> <pre class="brush:php;toolbar:false;">dsKey aus '@/injectionKeys/documentSet' importieren const ds = inject(dsKey) const documentSet = ds?.documentSet</pre> <p>Das Problem besteht darin, dass ich das Gefühl habe, etwas falsch zu machen, da es eine Menge Standardcode gibt.Ich kann zum Beispiel nicht verstehen, warum das DS-Objekt wie folgt umgestaltet werden sollte: </p> <pre class="brush:php;toolbar:false;">const { documentSet } = ds</pre> <p>Ist es möglich, den Code zu kürzen, anstatt <code>ds?.documentSet</code> zu schreiben, insbesondere wenn ich sicher bin, dass ds vorhanden sein sollte (ohne ihn werde ich die untergeordnete Komponente nicht rendern)< ;/p> <p>Wenn ich <code>const { documentSet } = inject(dsKey)</code> schreibe, erscheint die folgende Fehlermeldung: </p> <p><code>TS2339: Eigenschaft 'documentSet' existiert nicht für Typ '{ documentSet: Ref ;' Update: () => Ungültig '.</code></p> <p>Die Idee besteht darin, dass ich Duplikate in untergeordneten Komponenten beseitigen möchte:</p> <pre class="brush:php;toolbar:false;"><DocumentSetInfo :documentSet="documentSet" /> <DocumentSetMemos :documentSet="documentSet" /> <DocumentSetPrograms :documentSet="documentSet" /> <DocumentSetPetition :documentSet="documentSet" /> <DocumentSetPassRequests :documentSet="documentSet" /> <DocumentSetReport :documentSet="documentSet" /> <DocumentSetReceptionNotices :documentSet="documentSet" /></pre></p>
P粉441076405P粉441076405385 Tage vor475

Antworte allen(1)Ich werde antworten

  • P粉446800329

    P粉4468003292023-09-05 09:24:21

    你可以将它放入一个可组合项中,该项提供了一个处理提供的函数provde和一个处理注入的函数use*:

    const CurrentDocumentSetKey = Symbol() as InjectionKey<{
      documentSet: Ref<DocumentSet>,
      update: () => void
    }>
    
    export function provideCurrentDocumentSet(documentSet:DocumentSet, update: () => any){
      provide(CurrentDocumentSetKey, {documentSet, update})
    }
    
    export useCurrentDocumentSet(){
      const ds = inject(CurrentDocumentSetKey)
      if (!ds) throw new Error('No current DocumentSet provided')
      return ds
    }

    现在你只需要在父组件中调用provideCurrentDocumentSet(),所有子组件只需要执行以下操作:

    const {documentSet, update} = useCurrentDocumentSet()

    你甚至不需要导出InjectionKey,组件也不需要知道它。(例如Vuetify也是这样做的,例如这里

    Antwort
    0
  • StornierenAntwort