Alex Kyriakidis
Author - Educator - Consultant
<div id="app">
{{ message.split('').reverse().join('') }}
</div>
<div id="app">
<h1>{{ message.split('').reverse().join('') }}</h1>
<input v-model="message">
<div>{{ message.split('').reverse().join('') }}</div>
<span>{{ message.split('').reverse().join('') }} </span>
</div>
<script>
Vue.createApp({
data() {
return {
message: 'To infinity and beyond'
}
},
computed: {
reversedMessage () {
return this.message.split('').reverse().join('')
}
}
}).mount('#app')
</script>
<div id="app">
{{ reversedMessage }}
</div>
They are used to:
<div id="app">
<h1>{{ reversedMessage }}</h1>
<input v-model="message">
<div>{{ reversedMessage }}</div>
<span>{{ reversedMessage }} </span>
</div>
<script>
Vue.createApp({
data() {
return {
message: 'To infinity and beyond'
}
},
computed: {
reversedMessage () {
return this.message.split('').reverse().join('')
}
}
}).mount('#app')
</script>
<div id="app">
<p v-if="isSunny">It's sunny today!</p>
</div>
<script>
Vue.createApp({
data() {
return {
weather: 'sunny'
}
},
computed: {
isSunny () {
return this.weather === 'sunny'
}
}
}).mount('#app')
</script>
<div id="app">
<p v-if="isSunny">It's sunny today!</p>
</div>
<script>
Vue.createApp({
data() {
return {
weather: 'sunny'
}
},
computed: {
isSunny () {
return this.weather === 'sunny'
}
}
}).mount('#app')
</script>
<div id="app">
<p v-for="item in itemsInStock" :key="item.id">{{ item.name }}</p>
</div>
<script>
Vue.createApp({
data() {
return {
items: [
{id: 1, name: 'Hat', quantity: 7, price: 498},
{id: 2, name: 'Sock', quantity: 0, price: 59},
{id: 3, name: 'Shoes', quantity: 4, price: 1189}
]
}
},
computed: {
itemsInStock() {
return this.items.filter(item => item.quantity > 0)
}
}
}).mount('#app')
</script>
<div id="app">
<p v-for="item in itemsInStock" :key="item.id">{{ item.name }}</p>
</div>
<script>
Vue.createApp({
data() {
return {
items: [
{id: 1, name: 'Hat', quantity: 7, price: 498},
{id: 2, name: 'Sock', quantity: 0, price: 59},
{id: 3, name: 'Shoes', quantity: 4, price: 1189}
]
}
},
computed: {
itemsInStock() {
return this.items.filter(item => item.quantity > 0)
}
}
}).mount('#app')
</script>
<div id="app">
<p>Cart total: {{ shoppingCartTotal }}</p>
</div>
<script>
Vue.createApp({
data() {
return {
cart: [
{name: 'Hat', quantity: 1, price: 498},
{name: 'Sock', quantity: 3, price: 59},
{name: 'Shoes', quantity: 1, price: 1189}
]
}
},
computed: {
shoppingCartTotal() {
return this.cart.map(item => item.price * item.quantity)
.reduce((total, amount) => total + amount)
}
}
}).mount('#app')
</script>
<div id="app">
<p>Cart total: {{ shoppingCartTotal }}</p>
</div>
<script>
Vue.createApp({
data() {
return {
cart: [
{name: 'Hat', quantity: 1, price: 498},
{name: 'Sock', quantity: 3, price: 59},
{name: 'Shoes', quantity: 1, price: 1189}
]
}
},
computed: {
shoppingCartTotal() {
return this.cart.map(item => item.price * item.quantity)
.reduce((total, amount) => total + amount)
}
}
}).mount('#app')
</script>
<div id="app">
<h1>Total is {{amountInDollars}}</h1>
</div>
<script>
Vue.createApp({
data() {
return {
amount: 344
}
},
computed: {
amountInDollars () {
return `$${this.amount}.00`
}
}
}).mount('#app')
</script>
<div id="app">
<h1>Total is {{amountInDollars}}</h1>
</div>
<script>
Vue.createApp({
data() {
return {
amount: 344
}
},
computed: {
amountInDollars () {
return `$${this.amount}.00`
}
}
}).mount('#app')
</script>
computed: {
itemsInStock() {
return this.items.filter(
item => item.quantity > 0
)
}
}
methods: {
setWeather (weather) {
this.weather = weather
}
}
→
⚠️ JavaScript sort alters the original array
💡computed properties should not have side effects
💡create a copy of the array or use a library like lodash
// Create a Vue application
const app = Vue.createApp({})
// Define a new global component called button-counter
app.component('button-counter', {
data() {
return {
count: 0
}
},
template: `
<button @click="count++">
You clicked me {{ count }} times.
</button>`
})
Vue 2
<div id="app">
<h1>{{ message | capitalize }}</h1>
</div>
Vue 3
BUT
Vue 3
also works in Vue 2
Vue 3
to implement "filter" functionality, we use methods and computed
<div id="app">
<h1>Total is {{amountInDollars}}</h1>
</div>
<script>
Vue.createApp({
data() {
return {
amount: 145
}
},
computed: {
amountInDollars () {
return `$${this.amount}.00`
}
}
}).mount('#app')
</script>
<div id="app">
<h1>Total is {{dollars(amount, 2)}}</h1>
</div>
<script>
Vue.createApp({
data() {
return {
amount: 145
}
},
methods: {
dollars (amount, decimals = 0) {
return '$' + this.amount.toFixed(decimals)
}
}
}).mount('#app')
</script>
1. create a computed property or a method that transforms a given timestamp to the current time
Example
timestamp: 1610643160
time: 17:52
Hint: Use the JavaScript Date to format the time or a 3rd party library of your choice.
Install it now 🙃
By Alex Kyriakidis