Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Created by Maximilian Schwarzmüller
Vue.createApp();
Vue.createApp({
data() {
return {
enteredValue: "",
courseGoals: ["Finish the course and learn Vue!"],
vueLink: "https://vuejs.org/",
};
},
});
const app = Vue.createApp({
data() {
return {
enteredValue: "",
courseGoals: ["Finish the course and learn Vue!"],
vueLink: "https://vuejs.org/",
};
},
// To reference variables in createApp use the this keyword!
methods: {
addGoal() {
this.goals.push(this.enteredValue);
this.enteredValue = "";
}
},
}).mount("#app");
<div id="app">
<div>
<label for="goal">Goal</label>
<input type="text" id="goal" v-model="enteredValue"/>
<button v-on:click="addGoal">Add Goal</button>
</div>
<ul>
<li v-for="goal in goals">{{ goal }}</li>
</ul>
</div>
<li v-for="goal in goals">{{ goal }}</li>
All values not between HTML tags must have a v-for: tag added
<a v-bind:href="vueLink">about Vue</a>
const HTMLtags = "<h2>Heading</h2>"
<p v-html="vueLink">{{ HTMLtags }}</p>
<button v-on:click="addGoal">Add Goal</button>
<!-- Only when middle mouse button is clicked -->
<button v-on:click.middle="reduce(5)">Add 5</button>
<!-- Only when ENTER is keydown -->
<button v-on:keydown.enter="addGoal">Add Goal</button>
<!-- Other Vue key codes: .enter .tab .tab .delete .esc .space .up .down .left .right -->
<form v-on:submit.prevent="submitForm">
<form v-on:submit.stop="submitForm">
<!-- longhand -->
<input type="text" v-bind:value="variableValue" v-on:input="inputVariableEvent">
<!-- shorthand -->
<input type="text" v-model="variableName">
<p>Your Name: {{fullname()}}</p>
computed: {
fullname() {
return this.name !== "" ? this.name + " " + "Boet" : "";
},
},
watch: {
// Value in data object to watch. It's value (this.name) is given as an argument.
name(value) {
// variables to change when value is changed
this.fullname = value + " Gates"
}
},
watch: {
data(newValue, oldValue) {
}
},
watch: {
counter(value) {
if (value >= 50|| value <=-50) {
this.counter = 0;
}
},
<button v-on:click="clickAdd(5)">Add 5</button>
<button @click="clickAdd(5)">Add 5</button>
<div v-bind:class="{ active: isActive }"></div>
<div :class="{ active: isActive }"></div>
<!-- Inline -->
<div :class="{ className: true/false, 'text-danger': hasError }"></div>
<!-- Ternary -->
<div v-bind:class="isActive ? activeClass : '', errorClass"></div>
<!-- Array -->
<div v-bind:class="[isActive ? activeClass : '', errorClass]"></div>
// Computed
computed: {
paraClasses() {
return {
visible: this.paragraphIsVisible,
hidden: !this.paragraphIsVisible,
};
}
},
<p v-if="goals.length === 0">Please add some goals!</p>
<ul v-else>
<li>{{goal}}</li>
</ul>
<p v-show="goals.length === 0">Please add some goals!</p>
<ul>
<li v-for="(goal, index) in goals">{{ goal }} - {{ index }}</li>
</ul>
person = {name: 'Bill', age: 65}
<ul>
<li v-for="(value, key, index) in person">{{ key }}: {{ person }}</li>
</ul>
<ul>
<li v-for="num in 100">{{ num }}</li>
</ul>
<ul>
<li v-for="(goal, index) in goals" @click="removeGoal(index)">{{ goal }}</li>
</ul>
<ul>
<li v-for="(goal, index) in goals" :key="index">{{ goal }}</li>
</ul>
Author: thijsBoet
Source Code: https://github.com/thijsBoet/vue-the-complete-guide
#vue #vuejs #javascript