add a computed value
This commit is contained in:
parent
55d8a7d5d6
commit
6157d34472
|
@ -2,26 +2,44 @@
|
||||||
<section class="container">
|
<section class="container">
|
||||||
<h2>{{ user.name }}</h2>
|
<h2>{{ user.name }}</h2>
|
||||||
<h2>{{ user.age }}</h2>
|
<h2>{{ user.age }}</h2>
|
||||||
|
<button @click="setAge">Change Age</button>
|
||||||
|
<div>
|
||||||
|
<input type="text" placeholder="First Name" @input="setFirstName" />
|
||||||
|
<input type="text" placeholder="Last Name" @input="setLastlName" />
|
||||||
|
</div>
|
||||||
</section>
|
</section>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { reactive } from 'vue';
|
import { reactive, ref, computed } from 'vue';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
setup() {
|
setup() {
|
||||||
const user = reactive({
|
const user = reactive({
|
||||||
name: 'Foo',
|
|
||||||
age: 23
|
age: 23
|
||||||
});
|
});
|
||||||
|
const firstName = ref('');
|
||||||
|
const lastName = ref('');
|
||||||
|
|
||||||
setTimeout(function() {
|
user.name = computed(function() {
|
||||||
user.name = 'baz';
|
return firstName.value + ' ' + lastName.value;
|
||||||
|
});
|
||||||
|
|
||||||
|
function setAge() {
|
||||||
user.age = '25';
|
user.age = '25';
|
||||||
}, 2000);
|
}
|
||||||
|
|
||||||
|
function setFirstName(event) {
|
||||||
|
firstName.value = event.target.value;
|
||||||
|
}
|
||||||
|
function setLastlName(event) {
|
||||||
|
lastName.value = event.target.value;
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
user
|
user,
|
||||||
|
setAge,
|
||||||
|
setFirstName,
|
||||||
|
setLastlName
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Reference in New Issue