The structure of a Vue component

A Vue component is simply a file with an exported object containing properties that define how that component should look and behave. The properties must be given names that adhere to the Vue API. You can read about these properties and other aspects of the Vue API at https://vuejs.org/v2/api. The following code represents an example component containing the elements of the Vue API used in this application:

import SomeComponent from './SomeComponent.js';

export default {
name: 'dummy-component',

// Props passed from other components:
props: {
label: String,
},

// Other Vue components to render within the template:
components: {
SomeComponent
},

// Used to store local data/state:
data() {
return {
amount: 0
}
},

// Used to store complex logic that outside of the `template`:
computed: {
negativeClass() {
return {
'negative': this.amount < 0
};
}
},

// Methods that can be performed within the component:
methods: {
addOne() {
this.amount += 1;
}
},

// Perform actions if the local data changes:
watch: {
amount(val, oldVal) {
console.log(`New: ${val} | Old: ${oldVal}`);
}
},

// Contains the HTML to render the component:
template: `
<div>
<some-component></some-component>
<label for="someAmount">{{ label }}</label>
<input
id="someAmount"
:class="negativeClass"
v-model="amount"
type="number"
/>
<button @click="addOne">Add One</button>
</div>
`
};

The comments above each property describe its purpose, albeit at a very high level. Let's see Vue in action by reviewing the App component.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset