|
<template> |
|
<h2>My Course Goal</h2> |
|
<p> |
|
{{ goal1 }} |
|
</p> |
|
<!-- Task 1: Output your main course goal with help of the composition API --> |
|
<!-- Don't hardcode it into the template, instead hardcode it into the JS code --> |
|
<h3 v-show="isCourseGoalVisible">OUTPUT COURSE GOAL</h3> |
|
<!-- Task 2: Toggle (show/ hide) the goal with help of the button --> |
|
<button @click="handlers.handleToggleGoalBtn">Toggle Goal</button> |
|
<!-- Task 3: Manage data in three ways --> |
|
<!-- => Separate refs --> |
|
<!-- => Ref Object --> |
|
<!-- => Reactive Object --> |
|
<!-- Task 4: Also solve the assignment with the Options API --> |
|
<hr> |
|
<p> |
|
{{ myReactive.goal }} |
|
</p> |
|
<h3 v-show="myReactive.isVisible">OUTPUT COURSE GOAL</h3> |
|
|
|
<hr> |
|
<p> |
|
{{ myRef.goal }} |
|
</p> |
|
<h3 v-show="myRef.isVisible">OUTPUT COURSE GOAL</h3> |
|
</template> |
|
|
|
<script> |
|
import { ref, reactive } from "vue" |
|
|
|
export default { |
|
|
|
setup(props, context) { |
|
// context |
|
// context.emit('my-custom-event') instead of this.$emit('my-custom-event') |
|
// context.attr falltrhough attr |
|
// context. Slots |
|
const myGoal = ref("Learn Vue") |
|
const isCourseGoalVisible = ref(true) |
|
|
|
const myRef = ref({ |
|
goal: "Learn Vue", |
|
isVisible: true |
|
}) |
|
|
|
const myReactive = reactive({ |
|
goal: "Learn Vue", |
|
isVisible: true |
|
}) |
|
|
|
const delayInMs = 1000 |
|
setTimeout(() => { |
|
myGoal.value = "Build cool applications with Vue" |
|
myReactive.goal = "Build cool applications with Vue" |
|
myRef.value.goal = "Build cool applications with Vue" |
|
}, delayInMs) |
|
|
|
function handleToggleGoalBtn(event) { |
|
console.log("event", event) |
|
isCourseGoalVisible.value = !isCourseGoalVisible.value |
|
myReactive.isVisible = !myReactive.isVisible |
|
myRef.value.isVisible = !myRef.value.isVisible |
|
} |
|
|
|
const eventHandlers = { |
|
handleToggleGoalBtn |
|
} |
|
|
|
// making props properties reactive (props are reactive, but the properties are non-reactive) |
|
// afaic toRefs is enough b/c props is already reactive |
|
// const propsWithRefs = toRefs(props); |
|
// const user = propsWithRefs.user; |
|
const { user } = toRefs(props); |
|
watch(user, function ( ) { |
|
enteredSearchTerm. value = '' |
|
}) |
|
|
|
return { |
|
goal1: myGoal, isCourseGoalVisible, handlers: eventHandlers, myReactive, myRef |
|
} |
|
} |
|
} |
|
</script> |
|
|
|
<style> |
|
html { |
|
font-family: sans-serif; |
|
} |
|
|
|
body { |
|
margin: 3rem; |
|
text-align: center; |
|
} |
|
</style> |