Skip to content

Instantly share code, notes, and snippets.

@Kcko
Last active April 17, 2024 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kcko/ec6fc2a3780f695672d3a0ec9dec4215 to your computer and use it in GitHub Desktop.
Save Kcko/ec6fc2a3780f695672d3a0ec9dec4215 to your computer and use it in GitHub Desktop.
<script setup>
import { ref, watch, toRefs } from 'vue'
const props = defineProps({
test: String
})
let refTest = ref(props.test)
const { test: testToRefs } = toRefs(props)
watch(() => props.test, (newValue) => {
refTest.value = newValue
})
</script>
<template>
<div>
All ways how to have reactive props ...
<hr />
child: <br />
<hr />
test -> {{ test }} <br />
testToRefs -> {{ testToRefs }} <br />
$props.test -> {{ $props.test }} <br />
refTest-> {{ refTest }} <br />
</div>
</template>
<!--
after 3 seconds
test -> two
testToRefs -> two
$props.test -> two
refTest-> two
-->
<script setup>
import Child from './Child.vue'
import { ref } from 'vue'
const test = ref('one')
setTimeout(() => {
test.value = 'two'
}, 3000)
</script>
<template>
<h1>Parent</h1>
<Child :test="test"/>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment