VueJS Lekcja 2
This commit is contained in:
1 parent
e66b5ebc22
commit
0a308d6145
10 files changed
+373
-2
No files matched your search
@@ -0,0 +1,46 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Start VueJS</title>
|
||||
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="app">
|
||||
<p>1 - bez v-for</p>
|
||||
<ul>
|
||||
<li>{{ array[0] }}</li>
|
||||
<li>{{ array[1] }}</li>
|
||||
<li>{{ array[2] }}</li>
|
||||
</ul>
|
||||
|
||||
<p>2 - v-for</p>
|
||||
<ul>
|
||||
<li v-for="item in array">{{item}}</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li v-for="item of array2">{{item}}</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li v-for="(item, i) in array2">{{i}}: {{item}}</li>
|
||||
</ul>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
<script>
|
||||
|
||||
const app = Vue.createApp({
|
||||
data() {
|
||||
return {
|
||||
array: [1, 2, 3, 4, 5],
|
||||
array2: ["d", "b", "c", "a"]
|
||||
}
|
||||
}
|
||||
}).mount("#app");
|
||||
|
||||
</script>
|
||||
|
||||
</html>
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Start VueJS</title>
|
||||
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="app">
|
||||
<p>ul 1 - obiekty tablicy</p>
|
||||
<ul>
|
||||
<li v-for="item in array">{{ item }}</li>
|
||||
</ul>
|
||||
|
||||
<p>ul 2 - wartości obiektów tablicy</p>
|
||||
<ul>
|
||||
<li v-for="item in array">{{ item.a }} {{ item.b }}</li>
|
||||
</ul>
|
||||
|
||||
<p>ul 3 - wartości obiektów tablicy z licznikiem</p>
|
||||
<ul>
|
||||
<li v-for="(item, i) in array">{{i}}: {{ item.a }} {{ item.b }}</li>
|
||||
</ul>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
<script>
|
||||
|
||||
const app = Vue.createApp({
|
||||
data() {
|
||||
return {
|
||||
array: [{ a: 1, b: 4 }, { a: 2, b: 5 }, { a: 3, b: 6 }]
|
||||
}
|
||||
}
|
||||
}).mount("#app");
|
||||
|
||||
</script>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,41 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Start VueJS</title>
|
||||
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="app">
|
||||
<p>ul 1 - value, key obiektu</p>
|
||||
<ul>
|
||||
<li v-for="(value, key) of object">{{ key }} - {{ value }}</li>
|
||||
</ul>
|
||||
|
||||
<p>ul 2 - key, value, index obiektu</p>
|
||||
<ul>
|
||||
<li v-for="(value, key, index) in object">{{ index }}: {{ key }} - {{ value }}</li>
|
||||
</ul>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
<script>
|
||||
|
||||
const app = Vue.createApp({
|
||||
data() {
|
||||
return {
|
||||
object: {
|
||||
a: 111,
|
||||
b: 222,
|
||||
c: 333
|
||||
}
|
||||
}
|
||||
}
|
||||
}).mount("#app");
|
||||
|
||||
</script>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,51 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Start VueJS</title>
|
||||
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="app">
|
||||
<p>tekst: {{ text }}</p>
|
||||
<p>length: {{ length }}</p>
|
||||
|
||||
<h2>długość tekstu: {{ getTextLength2() }}</h2>
|
||||
<h2>długość tekstu: {{ getTextLength(text) }}</h2>
|
||||
|
||||
<h2 v-text="getTextLength(text)"></h2>
|
||||
<h2 v-html="getTextLengthWithHTML()"></h2>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
<script>
|
||||
|
||||
const app = Vue.createApp({
|
||||
data() {
|
||||
return {
|
||||
text: "to jest tekst",
|
||||
length: 0
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
getTextLength(val) {
|
||||
return val.length
|
||||
},
|
||||
getTextLength2() {
|
||||
return this.text.length
|
||||
},
|
||||
getTextLengthWithHTML() {
|
||||
return "długość text: " + this.text.length
|
||||
},
|
||||
setLength(val) {
|
||||
this.length = val
|
||||
}
|
||||
}
|
||||
}).mount("#app");
|
||||
|
||||
</script>
|
||||
|
||||
</html>
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Start VueJS</title>
|
||||
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="app">
|
||||
<h3>Lista produktów ({{ products.length }} szt.):</h3>
|
||||
<ul>
|
||||
<li v-for="product in products">{{ product.name }} (ID: {{ product.id }}) | Cena: {{ product.price }} |
|
||||
Kategoria: {{ product.category }}
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h2>Operacje na danych</h2>
|
||||
<h3>1. Produkty premium (Cena > 50 zł) - filter():</h3>
|
||||
<ul>
|
||||
<li v-for="product in getPremiumProducts()">{{ product.name }} - {{ product.price }} zł</li>
|
||||
</ul>
|
||||
<p>Łączna liczba produktów Premium: {{ getPremiumProducts().length }}</p>
|
||||
|
||||
<h3>2. Lista nazw i zmodyfikowanych cen (obniżka 10%) - map()</h3>
|
||||
<ul>
|
||||
<li v-for="product in discount()">{{ product.name }} | Nowa Cena: {{ product.price }} zł</li>
|
||||
</ul>
|
||||
|
||||
<h3>3. Całkowita wartość magazynu (Suma cen) - reduce():</h3>
|
||||
<p>Całkowity koszt wszystkich produktów: {{ suma() }} zł</p>
|
||||
|
||||
<h3>4. Wartość produktów elektronicznych - filter() + reduce():</h3>
|
||||
<p>Suma cen produktów z kategorii "Elektronika" (Filtrowanie + Sumowanie): {{ sumaElektro() }} zł</p>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
<script>
|
||||
|
||||
const app = Vue.createApp({
|
||||
data() {
|
||||
return {
|
||||
products: [
|
||||
{ id: 1, name: "Książka 'VueJS'", price: 35.99, category: "Książki" },
|
||||
{ id: 2, name: "Słuchawki Pro", price: 129.99, category: "Elektronika" },
|
||||
{ id: 3, name: "Myszka bezprzewodowa", price: 49.50, category: "Elektronika" },
|
||||
{ id: 4, name: "Zeszyt A4", price: 8.00, category: "Artykuły" },
|
||||
{ id: 5, name: "Monitor 4K", price: 999.00, category: "Elektronika" }
|
||||
]
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
getPremiumProducts() {
|
||||
const tablica = this.products;
|
||||
return tablica.filter(e => e.price > 50);
|
||||
},
|
||||
discount() {
|
||||
const tablica = this.products;
|
||||
return tablica.map(e => ({ name: e.name, price: (e.price * 0.9).toFixed(2) }));
|
||||
},
|
||||
suma() {
|
||||
const tablica = this.products;
|
||||
return tablica.reduce((acc, e) => acc + e.price, 0);
|
||||
},
|
||||
sumaElektro() {
|
||||
const tablica = this.products;
|
||||
const elektro = this.products.filter(e => e.category == 'Elektronika');
|
||||
return elektro.reduce((acc, e) => acc + e.price, 0);
|
||||
}
|
||||
}
|
||||
}).mount("#app");
|
||||
|
||||
</script>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,45 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Start VueJS</title>
|
||||
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="app">
|
||||
<p>computed w odróżnieniu od methods wykonują się tylko raz i nie używamy nawiasów podczas ich wywoływania</p>
|
||||
<h2>{{ getCar }} </h2>
|
||||
<h2>{{ getYear }} </h2>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
<script>
|
||||
|
||||
const app = Vue.createApp({
|
||||
data() {
|
||||
return {
|
||||
type: "Fiat",
|
||||
year: 1989,
|
||||
model: "Panda"
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
getCar() {
|
||||
const car = "To jest auto: " + this.type + " " + this.model;
|
||||
console.log(car);
|
||||
return car;
|
||||
},
|
||||
getYear() {
|
||||
const year = "To jest rok: " + this.year;
|
||||
console.log(year);
|
||||
return year;
|
||||
}
|
||||
}
|
||||
}).mount("#app");
|
||||
|
||||
</script>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,29 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Start VueJS</title>
|
||||
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="app">
|
||||
|
||||
</div>
|
||||
</body>
|
||||
|
||||
<script>
|
||||
|
||||
const app = Vue.createApp({
|
||||
data() {
|
||||
return {
|
||||
|
||||
}
|
||||
}
|
||||
}).mount("#app");
|
||||
|
||||
</script>
|
||||
|
||||
</html>
|
||||
Reference in new issue
Block a user