示例
App.vue
<template>
<section class="container">
<h2>{{ user.userName }}</h2>
<h3>{{ user.age }}</h3>
</section>
</template>
<script>
import { ref, reactive, toRefs, isReactive, isRef } from 'vue'; // reactive used for object
export default {
setup() {
const uAge = ref(31);
const user = reactive({
userName: 'Maximilian',
age: 20,
});
console.log(isReactive(user)); // true
console.log(isRef(uAge)); // true
const userRefs = toRefs(user); //makes nested object properties reactive
setTimeout(() => {
user.userName = 'Max';
user.age = 31;
}, 2000);
return {
user: user
};
}
};
</script>
<style>
* {
box-sizing: border-box;
}
html {
font-family: sans-serif;
}
body {
margin: 0;
}
.container {
margin: 3rem auto;
max-width: 30rem;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.26);
padding: 1rem;
text-align: center;
}
</style>