@@ -299,6 +299,144 @@ transition: fade-out
299299transition: fade-out
300300---
301301
302+ # APIs im Vergleich
303+
304+ - Options API
305+ - simpler zum Einstieg
306+
307+ <div v-click =" 1 " >
308+
309+ - Composition API
310+ - besser für größere Projekte
311+ - bessere Strukturierung (nach Logik, nicht nach Typ)
312+ - bessere Wiederverwendbarkeit (Composables statt Mixins)
313+ - bessere Typisierung (Typescript)
314+ - bessere Performance und Effizienz (Vapor Mode ohne Shadow-DOM, Bundle-Size)
315+ - neue Features, Libraries und Docs meistens für Composition API angepasst oder exklusiv
316+
317+ </div >
318+
319+ ---
320+ transition: fade-out
321+ ---
322+
323+ # Strukturierung
324+
325+ <div class =" flex justify-center " >
326+ <img src =" /assets/compositionApi.png " class =" h-100 " />
327+ </div >
328+
329+ ---
330+ transition: fade-out
331+ ---
332+
333+ ```` md magic-move {lines: true}
334+ ```vue
335+ <script> // Options API
336+ export default {
337+ props: { initialItems: { type: Array, required: true } },
338+ data() {
339+ return { items: [], searchTerm: '', lastSearchTime: null }
340+ },
341+ computed: {
342+ filteredItems() { return this.items
343+ .filter(i => i.name.toLowerCase().includes(this.searchTerm.toLowerCase())) },
344+ totalItems() { return this.items.length },
345+ favoriteCount() { return this.items.filter(i => i.favorite).length }
346+ },
347+ methods: {
348+ toggleFavorite(item) {
349+ item.favorite = !item.favorite
350+ this.$emit('favorite-changed', item)
351+ },
352+ logSearch() {
353+ this.lastSearchTime = new Date()
354+ console.log(`Search at ${this.lastSearchTime}`)
355+ }
356+ },
357+ created() {
358+ this.items = this.initialItems.map(item => ({ ...item, favorite: false }))
359+ }
360+ }
361+ </script>
362+ ```
363+
364+ ```vue {*|5-7|9-13|15-25}
365+ <script setup> // Composition API
366+ const props = defineProps({ initialItems: { type: Array, required: true } })
367+ const emit = defineEmits(['favorite-changed'])
368+
369+ const items = ref([])
370+ onMounted(() => { items.value = props.initialItems.map(item => ({...item, favorite: false})) })
371+ const totalItems = computed(() => items.value.length)
372+
373+ const favoriteCount = computed(() => items.value.filter(i => i.favorite).length)
374+ function toggleFavorite(item) {
375+ item.favorite = !item.favorite
376+ emit('favorite-changed', item)
377+ }
378+
379+ const searchTerm = ref('')
380+ const lastSearchTime = ref(null)
381+ const filteredItems = computed(() =>
382+ items.value.filter(item =>
383+ item.name.toLowerCase().includes(searchTerm.value.toLowerCase())
384+ )
385+ )
386+ function logSearch() {
387+ lastSearchTime.value = new Date()
388+ console.log(`Search at ${lastSearchTime.value}`)
389+ }
390+ </script>
391+ ```
392+
393+ ```ts
394+ export function useSearch(items) { // Composable
395+ const searchTerm = ref('')
396+ const lastSearchTime = ref(null)
397+
398+ const filteredItems = computed(() =>
399+ items.value.filter(i => i.name.toLowerCase().includes(searchTerm.value.toLowerCase()))
400+ )
401+
402+ function logSearch() {
403+ lastSearchTime.value = new Date()
404+ console.log(`Search at ${lastSearchTime.value}`)
405+ }
406+
407+ return {
408+ searchTerm,
409+ filteredItems,
410+ lastSearchTime,
411+ logSearch
412+ }
413+ }
414+ ```
415+
416+ ```vue
417+ <script setup> // Composition API
418+ const props = defineProps({ initialItems: { type: Array, required: true } })
419+ const emit = defineEmits(['favorite-changed'])
420+
421+ const items = ref([])
422+ onMounted(() => { items.value = props.initialItems.map(item => ({...item, favorite: false})) })
423+ const totalItems = computed(() => items.value.length)
424+
425+ const favoriteCount = computed(() => items.value.filter(i => i.favorite).length)
426+ function toggleFavorite(item) {
427+ item.favorite = !item.favorite
428+ emit('favorite-changed', item)
429+ }
430+
431+ const { searchTerm, filteredItems, logSearch } = useSearch(items)
432+ </script>
433+ ```
434+ ````
435+
436+ ---
437+ transition: fade-out
438+ ---
439+
302440# Pinia
303441
304442- Empfohlene Alternative zu Vuex
0 commit comments