<!-- Component template --><!-- ------------------ --><template><divclass="component"><!-- Iterate through all items and display in the list --><ulclass=""><liv-for="item in items":key="item.id">{{item.title}}</li></ul><!-- Reference a child component with property and a click event calling a function --><divclass="childComponent"><Child-Component:childProperty="title"@clicked="doSometnig"class=""></Child-Component></div><!-- Display this title if the array of items is empty --><h3v-if="!items.length">Time to add new items!</h3></div></template><!-- Define logic and data --><!-- --------------------- --><script>// To use Child-Component you need to import it firstimportChild-Componentfrom'./Child-Component';exportdefault{// If properties are being passed to this component from the parent you need to declare them hereprops:{propOne:{type:String,// String / Array / Number / Booleanrequired:true,// true / falsedefault:''}},// Declare all data for this component heredata(){return{title:'',// Stringcount:0,// Numberitems:[],// Array: []componentState:true// Boolean: true / false}},// When the component if fully loaded run methods declared belowmounted(){console.log(this.items);// Fetch data from an API using axiosaxios.get(`/api/items/`).then(response=>{this.items=response.data;}).catch(function(error){console.log(error);});},// Add methodsmethods:{doSometnig(){console.log(this.title);}},// Register loaded componentscomponents:{Child-Component// , Second-Component...}}</script>
Create new Vue project in the current directory using Vue CLI command. To quickly create a project using default settings add "--default" flag at the end of the command.
Vue on click directive listens for a click event on a given element. You can reference a method to fire on each click. Use v-on:click or a shorthand @click, they work exactly the same way.