-
-
Notifications
You must be signed in to change notification settings - Fork 4
Vue Introduction
Vue is a versatile and beginner-friendly JavaScript framework. This introduction is only going to cover what is relevant to our codebase.
A typical .vue file includes;
- A
<template>block for HTML markup - A
<script>block for js logic - A
<style>block for css styling
Like this:
<template>
<div class="wrapper">
<!-- Child content here -->
</div>
</template>
<script>
// Logic
</script>
<style scoped>
/* Styles */
</style>Note
<style scoped> limits the styling to the current component
frontend/src/components/AutonWaypointEditor.vue
<WaypointStore
v-for="(waypoint, index) in waypoints"
:key="waypoint"
:waypoint="waypoint"
:index="index"
@add="addItem"
@delete="deleteMapWaypoint"
/>
</div>Only render the element if the condition is true.
frontend/src/components/AutonWaypointItem.vue
<button
v-if="!enable_costmap"
class="btn btn-danger"
@click="toggleCostmap"
>
Costmap
</button>
<button
v-if="enable_costmap"
class="btn btn-success"
@click="toggleCostmap"
>
Costmap
</button>Bind a function to an event like click.
frontend/src/components/CameraFeed.vue
<canvas :id="'stream-' + id" v-on:click="handleClick"></canvas>Define component-local variables using the data() function.
data() {
return {
x: 1,
arr: [1, 2, 3]
}
}Define functions you want to call from your template or internally.
methods: {
addOnetoX() {
this.x = this.x + 1
}
}Used for derived or reactive values based on existing data.
computed: {
xPlusTwo() {
return this.x + 2
}
}Run code in response to changes in a specific data property.
watch: {
x(val) {
console.log("x has changed")
}
}Runs before the component is initialized.
beforeCreate() {
// setup logic here
}Runs after the component is initialized, but before DOM is mounted.
created() {
// fetch data, set up reactive properties, etc.
}Runs after the component is added to the DOM.
mounted() {
// DOM-dependent logic
}Runs after any DOM update caused by reactive changes.
updated() {
// respond to reactive updates
}Runs when the component is removed from the DOM. Great for cleanup.
unmounted() {
// cleanup logic, remove event listeners
}Import other files for reuse.
import Component from './Component.vue'Warning
Ever since Vuex 4, instead of this, which gives a warning
import { mapGetters } from 'vuex'
You should do this instead
import Vuex from 'vuex'
const { mapGetters } = 'vuex'
Register child components that you want to use inside this component. If you don't register it, Vue won't recognize it.
components: {
Component
}