Skip to content

Vue.js ​

Бсылка: https://vuejs.org/

🧠 Π‘ΡƒΡ‚ΡŒ ​

ΠŸΡ€ΠΎΠ³Ρ€Π΅ΡΡΠΈΠ²Π½Ρ‹ΠΉ JS-Ρ„Ρ€Π΅ΠΉΠΌΠ²ΠΎΡ€ΠΊ для создания UI. Основа VitePress, Nuxt, ΠΈ мноТСства Π΄Ρ€ΡƒΠ³ΠΈΡ… инструмСнтов.

⚑ Composition API ​

vue
<script setup>
import { ref, computed, watch } from "vue";

const count = ref(0);
const doubled = computed(() => count.value * 2);

watch(count, (newVal) => {
  console.log("Count changed:", newVal);
});
</script>

<template>
  <button @click="count++">{{ doubled }}</button>
</template>

πŸ’» Π Π΅Π°ΠΊΡ‚ΠΈΠ²Π½ΠΎΡΡ‚ΡŒ ​

ts
import { ref, reactive, computed, watch, watchEffect } from "vue";

// ref β€” для ΠΏΡ€ΠΈΠΌΠΈΡ‚ΠΈΠ²ΠΎΠ²
const name = ref("Vue");

// reactive β€” для ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ²
const user = reactive({ name: "John", age: 30 });

// computed β€” вычисляСмыС значСния
const fullName = computed(() => `${user.name} (${user.age})`);

// watch β€” слСТСниС Π·Π° измСнСниями
watch(name, (newVal) => console.log(newVal));

// watchEffect β€” Π°Π²Ρ‚ΠΎ-отслСТиваниС зависимостСй
watchEffect(() => {
  console.log(`Hello, ${name.value}!`);
});

πŸ’» ΠšΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚Ρ‹ ​

vue
<!-- Parent.vue -->
<script setup>
import Child from "./Child.vue";

const items = ref([1, 2, 3]);
</script>

<template>
  <Child :items="items" @update="handleUpdate" />
</template>
vue
<!-- Child.vue -->
<script setup>
const props = defineProps({ items: Array });
const emit = defineEmits(["update"]);

const handleClick = (item) => emit("update", item);
</script>

<template>
  <ul>
    <li v-for="item in items" @click="handleClick(item)">{{ item }}</li>
  </ul>
</template>

πŸ’» Provide / Inject ​

vue
<!-- Provider.vue -->
<script setup>
import { provide, ref } from "vue";

const theme = ref("dark");
provide("theme", theme);
</script>

<!-- Consumer.vue -->
<script setup>
import { inject } from "vue";

const theme = inject("theme", "light");
</script>

πŸ’» Lifecycle ​

ts
import { onMounted, onUpdated, onUnmounted } from "vue";

onMounted(() => {
  // DOM Π³ΠΎΡ‚ΠΎΠ²
});

onUpdated(() => {
  // ПослС обновлСния
});

onUnmounted(() => {
  // ΠžΡ‡ΠΈΡΡ‚ΠΊΠ° (Ρ‚Π°ΠΉΠΌΠ΅Ρ€Ρ‹, ΡΠ»ΡƒΡˆΠ°Ρ‚Π΅Π»ΠΈ)
});

πŸ’» Slots ​

vue
<!-- Card.vue -->
<template>
  <div class="card">
    <header><slot name="header" /></header>
    <main><slot /></main>
    <footer><slot name="footer" /></footer>
  </div>
</template>

⚠️ ΠŸΠΎΠ΄Π²ΠΎΠ΄Π½Ρ‹Π΅ ΠΊΠ°ΠΌΠ½ΠΈ ​

  • ref Π½ΡƒΠΆΠ΅Π½ .value Π² JS, Π² шаблонС β€” автоматичСски
  • reactive нСльзя Π·Π°ΠΌΠ΅Π½ΡΡ‚ΡŒ Ρ†Π΅Π»ΠΈΠΊΠΎΠΌ (Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΌΡƒΡ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ свойства)
  • watch Π½Π΅ срабатываСт ΠΏΡ€ΠΈ Π·Π°ΠΌΠ΅Π½Π΅ reactive-ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°
  • provide/inject Π½Π΅ Ρ€Π΅Π°ΠΊΡ‚ΠΈΠ²Π΅Π½ ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ (Π½ΡƒΠΆΠ΅Π½ ref/reactive)

πŸš€ Best Practice ​

  1. Π˜ΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠΉ <script setup> ΠΈ Composition API
  2. ref для ΠΏΡ€ΠΈΠΌΠΈΡ‚ΠΈΠ²ΠΎΠ², reactive для ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ²
  3. onUnmounted β€” всСгда чисти ΡΠ»ΡƒΡˆΠ°Ρ‚Π΅Π»ΠΈ ΠΈ Ρ‚Π°ΠΉΠΌΠ΅Ρ€Ρ‹
  4. ΠšΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚Ρ‹ β†’ малСнькиС ΠΈ ΠΏΠ΅Ρ€Π΅ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌΡ‹Π΅

Built with VitePress and ❀️