示例
App.vue
<template>
<section class="container">
<h2>{{ user.userName }}</h2>
<h3>{{ user.age }}</h3>
<button @click="setAge">Change Age</button>
</section>
</template>
<script>
import { reactive } from 'vue'; // reactive used for object
export default {
setup() {
const user = reactive({
userName: 'Maximilian',
age: 20,
});
function setNewAge(){
user.age = 32;
};
return {
user: user,
setAge: setNewAge
};
}
};
</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>