0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

vue3覚書(watch,watchEffect,ライフサイクル)

Posted at

watch

  • 値の変更を監視
  • numberの値が変わったら呼ばれる
<script setup lang="ts">
import { ref, watch} from 'vue';

const number = ref(100)

watch(number, () => {
  console.log("watch")
})

const incrementNumber = () => {
  number.value++
}
</script>

<template>
  <div>
    <div>
      {{ number }}
    </div>
    <div>
      <button @click="incrementNumber">increment</button>
    </div>
  </div>
</template>

第一引数に監視したいデータ、第二引数にコールバック関数を指定する

  • crには変更後の値が入る
  • prevには変更前の値が入る
watch(number, (cr, prev) => {
  console.log("number", number.value)
  console.log("cr", cr)
  console.log("prev", prev)
  console.log("number", number.value)
})

watchEffect

  • やれることはwatchと同じ
  • watchより簡単にかける
  • 自動的にコンポーネント内のreactiveな値が監視される
<script setup lang="ts">
import { ref, watchEffect} from 'vue';

const number = ref(100)

watchEffect(() => {
  console.log(number.value)
})

const incrementNumber = () => {
  number.value++
}
</script>

<template>
  <div>
    <div>
      {{ number }}
    </div>
    <div>
      <button @click="incrementNumber">increment</button>
    </div>
  </div>
</template>

ライフサイクル

コンポーネントの読み込み時

  • created
  • onBeforeMount
  • onMounted

リソース呼び出し時

  • onBeforeUpdate
  • onUpdated

コンポーネントの読み込み終了時

  • onBeforeMount
  • onUmounted

親コンポーネント

  • IsShowで子コンポーネントのマウント、アウンマウントを切り替え
<script setup lang="ts">
import { ref } from 'vue';
import LifeCycle from './components/LifeCycle.vue';

const IsShow = ref<Boolean>(false)
const ChangeIsShow = (Boolean: boolean) => {
  IsShow.value = Boolean
}
</script>

<template>
  <div>
    <LifeCycle v-if="IsShow"/>
    <button @click="ChangeIsShow(true)">show</button>
    <button @click="ChangeIsShow(false)">hide</button>
  </div>
</template>

子コンポーネント

<script setup lang="ts">
import { onBeforeMount, onBeforeUnmount, onBeforeUpdate, onMounted, onUnmounted, onUpdated, ref } from 'vue';
const text = ref<string>('aiueo')

// 読み込み時
onBeforeMount(() => {
  console.log("onBeforeMount")
})

onMounted(() => {
  console.log("onMounted")
})

onBeforeUpdate(() => {
  console.log("onBeforeUpdate", text.value)
})

// textに変更があったとき
onUpdated(() => {
  console.log("onUpdated", text.value)
})

// textが変更される前
onBeforeUnmount(() => {
  console.log("onBeforeUnmount")
})

// 読み込み終了時
onUnmounted(() => {
  console.log("onUnmounted")
})

console.log("created")
</script>

<template>
  <div>
    <h1>TestLifeCycle</h1>
    <div>
    <input type="text" v-model="text" placeholder="input test" />
    <div>{{ text }}</div>
    </div>
  </div>
</template>
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?