- import into html
<script src="https://unpkg.com/vue@3.4.9/dist/vue.global.js"></script>- const app = Vue.createApp({}).mount("#user-goal");
const app = Vue.createApp({
data() {
return {
courseGoal: "finish the course and learn vue",
vueLink: "https://vuejs.org/",
};
},
methods: {
outputGoal() {
const randomNumber = Math.random();
if (randomNumber < 0.5) {
return "learn vue";
} else {
return "master vue";
}
},
},
}).mount("#user-goal");- use data interpoliation between html tags
{{}}- data(),
- methods: {}
- use
v-bind:hreffor html attributes eg. - functions - if you define methods, you call it like a {{ method() }}
- from methods refer to data() attributes via
this
- BUT if it is an html string...use
v-html
v-on:click="add"
- methods by default receive in methods "event" -> event.target.value
- if you need the event AND to pass a value:
<input type="text" v-on:input="setName($event, 'something')" />then in javascript - it can be the first param
methods: {
setName(event, lastName);
}- method has event which you call
event.preventDefault()
<form v-on:submit="submitForm">
<input type="text" />
<button>Sign up</button>
</form>- vs. event modifier
- form handler .prevent is a modifier for form
<form v-on:submit.prevent="submitForm">
<form v-on:submit.prevent="submitForm">
<input type="text" />
<button>Sign up</button>
</form>- modifiers for reacting to mouse right-click only
<button v-on:click.right="reduce(5)">Reduce</button>- key modifier
- v-on:keyup.enter
<input
v-on:input="setName($event, 'surname')"
v-on:keyup.enter="confirmInput"
type="text"
/>- binding should only happen once
<p v-once>Starting counter: {{counter}}</p>v-bind:value="name"
<input
type="text"
v-bind:value="name"
v-on:input="setName($event, 'Schwarzmüller')"
/>-
v-model directive
-
2-way binding
-
replace with
<input type="text" v-model="name" />
-
when calling a computed property, you call it like {{fullname}} not {{ fullname() }}
-
vue is aware of the dependencies of computed properties, caches value, only recalculate and reevaluate if dependency changed.
-
only use methods when you want to recaculate a method when anything on a page changes (rarely the case)
-
you still bind events to methods (NOT to computed properties)
-
use computed properties to output
const app = Vue.createApp({
computed: {
fullname() {
if (this.name === "") {
return "";
}
return this.name + " " + "Schwarzmüller";
},
},
//...
});<p>Your Name: {{ fullname }}</p>- A function that runs automatically when a watched data or computed property changes.
- Defined inside a watch option e.g., watch: { name(newValue, oldValue){} }
- a watcher automatically gets the last value of data or computed property as an argument
- Useful when you want to perform side effects based on data changes, not compute values.
Examples:
- Reset a counter when it exceeds 50.
- Send an HTTP request when data changes.
- Trigger a timer or perform async actions.
- shorthand for 'v-on:' is @
<button @click=""></button>- shorthand for 'v-bind:' is to drop the 'v-bind' and just have the colon
<div
class="demo"
v-bind:style="{borderColor: boxCSelected ? 'red' : '#CCC'}"
@click="boxSelected('C')"
></div>- option 1 - using ternarary expression
<div
v-bind:class="{boxASelected ? 'demo active': 'demo'}"
@click="boxSelected('B')"
></div>- option 2 - use an object and add properties where property names are the css classes and values are true/false or truthy/falsy indicating whether class should be added
<div
class="demo"
v-bind:class="{active: boxASelected}"
@click="boxSelected('A')"
></div>- moving the classes to the computed properties
<div class="demo" v-bind:class="boxAClasses" @click="boxSelected('A')"></div>const app = Vue.createApp({
//...
computed: {
boxAClasses() {
return { active: this.boxASelected };
},
},
//...
});
app.mount("#styling");- using array syntax
<div
v-bind:class="['demo', {active: boxBSelected}]"
@click="boxSelected('B')"
></div><p v-if="goals.length === 0">
No goals have been added yet - please start adding some!
</p>
<ul v-else>
<li>Goal</li>
</ul>- with v-show, it only adds a 'display:none' (show/hide) - doesnt remove from DOM
<p v-show="goals.length === 0">NO GOAL</p><li v-for="goal in goals">{{goal}}</li>- using the index with v-for
<li v-for="(goal, index) in goals">{{goal}} - {{index}}</li>- looping through objects with v-for
<ul>
<li v-for="(value, key, index) in {name:'Max', age:31}">
{{key}} : {{value}} - {{index}}
</li>
</ul>- looping through numbers
<ul>
<li v-for="num in 10">{{num}}</li>
</ul>// app.js
const app = Vue.createApp({
data() {
return {
goals: [],
};
},
methods: {
removeGoal(index) {
this.goals.splice(index, 1);
},
},
});<ul v-else>
<li v-for="(goal, index) in goals" @click="removeGoal(index)">
{{goal}} - {{index}}
</li>
</ul>- using unique keys for v-for
<ul v-else>
<li
v-for="(goal, index) in goals"
v-bind:key="goal"
@click="removeGoal(index)"
>
{{goal}} - {{index}}
</li>
</ul>