Vue: General Concepts
Review and refresh.
Like React, Vue provides multiple ways to define a Component:
<script setup>(loosely analogous to a Stateless Dummy Function).- non-
<script setup>(export default {})
Unlike React, both support Lifecycle Hooks (the former within the
<script setup>Tags with the Composition API and the later withinsetup(props, context) {}using the Options API.)
Resources and Links
Code samples:
Vue: Components
Props
Vue vs. React:
- Passed from Parent to Child (as in React).
- Generally uni-directional but can be bound with
v-modeltwo-way.
Examples:
<script setup>
const props = defineProps(['myVar'])
console.log(props.myVar)
</script>
export default {
props: ['myVar'],
setup(props) {
console.log(props.myVar)
}
}
Computed Properties
For complex logic and Templating scenarios. Two general methods:
- Computed Properties/Caching
- Computed Methods
The following Computed Properties is only called in response to reactive changes (to myReactiveData, e.g. - Computed Caching):
<script setup>
import { reactive, computed } from 'vue'
// Suppose 'msg' is updated...
const myReactiveData = reactive({
id: 1,
msg: "my note"
})
const computedRefAndPropertyForReactiveMsg = computed(() => {
return myReactiveData.msg
})
</script>
<template>
<span>{{ computedRefAndPropertyForReactiveMsg }}</span>
</template>
By contrast, a Computed Methods is always called:
<script setup>
const computedRefAndPropertyAlwaysInvoked = () => {
return "I'm always invoked"
}
</script>
<template>
<span>{{ computedRefAndPropertyAlwaysInvoked }}</span>
</template>
State
<script>
export default {
data() {
return {
dataProperty: "I'm an initial Value - not a placeholder"
}
},
methods() {
myMethod() {
this.dataProperty = "I'm a set Value!"
}
}
}
</script>
Resources and Links
Vue: Components
Different syntax:
Vue.component('component-name', {
props: ['myProp'],
template: 'Rendering: <span>{{ myProp }}</span>'
})
<script setup>
// my code here
</script>
export default {
props: {},
setup(props, context) {}
}
Hooks
Lifecycle Hook example:
<script setup>
import { onMounted } from 'vue'
onMounted(() => {
loadExamples()
})
</script>
Unlike in React, both <script setup> and non-<script setup> Components support Lifecycle Hooks:
- Between
<script setup>Tags (use the Composition API e.g. -onMounted()) - Within the Scope of
setup(props, context) {}(use the Options API e.g. -mounted())
Setup Hook:
export default {
props: {},
setup(props, context) {
const myVal = ref("a")
return {
myVal
}
}
}
Use
refto associate Vue with changes tomyVal(unlike React).myValis returned and can be used elsewhere in the Template.
Lifecycle Hooks
Main Lifecyle stages:
- Created
- Only accessible through the Options API
- Instance creation but before Observables, (most) Event Listeners, DOM
- Mounted
- Added to DOM
- Updated
- Updating Values
- Unmounted
- Component cleanup
- Deattaching the Component from the DOM
Options API
Main Lifecyle stages:
created(beforeCreated)mounted(beforeMounted)updated(beforeUpdate)unmounted(beforeUnmount)
DOM Lifecycle stages:
activated,deactivatedrenderTracked,renderTriggered
Misc Lifecycle stages:
errorCapturedserverPrefetch
Composition API
Main Lifecyle stages:
onMounted(onBeforeMounted)onUpdated(onBeforeUpdated)onUnmounted(onBeforeUnmounted)
DOM Lifecycle stages:
onActivated,onDeactivatedonRenderTracked,onRenderTriggered
Misc Lifecycle stages:
onErrorCapturedonServerPrefetch
State
<script>
export default {
data() {
return {
dataProperty: "I'm an initial Value - not a placeholder"
}
},
methods() {
myMethod() {
this.dataProperty = "I'm a set Value!"
}
}
}
</script>
Resources and Links
- https://vuejs.org/api/composition-api-lifecycle.html
- https://vuejs.org/api/composition-api-lifecycle
- https://vuejs.org/api/options-state.html
Code samples:
Vue: Directives
v-show- Conditional Elemental rendering based on Truthiness of a supplied Property.
- Toggles:
display: none. - Immediate rendering.
v-show="myToggle"
v-bind- Bind an Attribute or Value to Expression or Prop.
- Shorthand
: - Example:
v-bind:class="classDataProperty(:class="classDataProperty)
v-on- Bind an Element to an Event Action.
- Shorthand
@ v-on:click="myToggle = !myToggle"(@click="myToggle = !myToggle")- Examples:
<div v-on:click="displayMessage"> {{ message }} </div>
v-model- Two way Form binding.
v-model="modelName"
v-for- Render List of Elements:
<div v-for="x in myList"> {{ x.id }} </div>
- Render List of Elements:
v-if- Conditionally render an Element or Template based on the Truthiness of the Expression Value.
- Removes or adds to DOM.
- Lazy loading.
- Can be combined with
v-else-ifandv-else.