Skip to content

Commit cfba4ee

Browse files
1. 增加 axios 練習範例
2. 加入每日任務 11
1 parent 690b9bc commit cfba4ee

File tree

7 files changed

+356
-5
lines changed

7 files changed

+356
-5
lines changed

package-lock.json

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"deploy": "vite build && gh-pages -d dist"
1313
},
1414
"dependencies": {
15+
"axios": "^1.7.3",
1516
"bootstrap": "^5.3.3",
1617
"pinia": "^2.1.7",
1718
"vue": "^3.4.29",

src/router/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ const router = createRouter({
6868
path: 'von',
6969
component: () => import('../views/samples/VOn.vue'),
7070
},
71+
{
72+
path: 'axios',
73+
component: () => import('../views/samples/AxiosTest.vue'),
74+
},
7175
],
7276
},
7377
// 每日任務
@@ -121,10 +125,10 @@ const router = createRouter({
121125
path: 'day10',
122126
component: () => import('../views/day_jobs/DayJob10.vue'),
123127
},
124-
// {
125-
// path: 'day11',
126-
// component: () => import('../views/day_jobs/DayJob11.vue'),
127-
// },
128+
{
129+
path: 'day11',
130+
component: () => import('../views/day_jobs/DayJob11.vue'),
131+
},
128132
// {
129133
// path: 'day12',
130134
// component: () => import('../views/day_jobs/DayJob12.vue'),

src/views/day_jobs/DayJob11.vue

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<template>
2+
<h1 class="h1">🏅 Day11 - Vue.js 的黑魔法:Watch</h1>
3+
<p>
4+
<a href="https://hackmd.io/5qENDoiZS261xzOlaSRL5w" target="_blank">題目</a>|
5+
<a
6+
href="https://github.com/GitHubPlayerZero/hex-vue3-practise/blob/main/src/views/day_jobs/DayJo11.vue"
7+
target="_blank"
8+
>Code</a
9+
>|
10+
<a href="https://codepen.io/codepenplayer/pen/VwJMGgP" target="_blank">CodePen</a>
11+
</p>
12+
13+
<hr class="hr mt-0" />
14+
15+
<h2 class="h2">方法一:監聽整個物件</h2>
16+
<p>需搭配深層監聽。</p>
17+
18+
<form class="box mb-5" @submit.prevent>
19+
<div class="m-2">
20+
<label for="username">購買人姓名:</label>
21+
<input id="username" v-model="user.username" />&nbsp;&nbsp;
22+
<span class="error" v-if="errors.username">{{ errors.username }}</span>
23+
</div>
24+
</form>
25+
26+
<h2 class="h2">方法二:監聽物件裡的純值屬性</h2>
27+
<p>需搭配使用 getter 函式作為監聽來源的寫法。</p>
28+
29+
<form class="box" @submit.prevent>
30+
<div class="m-2">
31+
<label for="username2">購買人姓名:</label>
32+
<input id="username2" v-model="user2.username" />&nbsp;&nbsp;
33+
<span class="error" v-if="errors2.username">{{ errors2.username }}</span>
34+
</div>
35+
</form>
36+
37+
<hr class="hr">
38+
39+
<h2 class="h2">即時監聽練習</h2>
40+
<p>一開始就會先自動執行監聽。</p>
41+
<form class="box" @submit.prevent>
42+
<div class="m-2">
43+
<label for="username3">購買人姓名:</label>
44+
<input id="username3" v-model="user3.username" />&nbsp;&nbsp;
45+
<span class="error" v-if="errors3.username">{{ errors3.username }}</span>
46+
</div>
47+
</form>
48+
49+
</template>
50+
51+
<script setup>
52+
import { ref, watch } from 'vue';
53+
54+
const user = ref({
55+
username: '',
56+
});
57+
const errors = ref({
58+
username: '',
59+
});
60+
61+
const user2 = ref({
62+
username: '',
63+
});
64+
const errors2 = ref({
65+
username: '',
66+
});
67+
68+
const user3 = ref({
69+
username: '',
70+
});
71+
const errors3 = ref({
72+
username: '',
73+
});
74+
75+
76+
const checkName = (name) => {
77+
if (name.length != 3) {
78+
return "需要正確的輸入名稱";
79+
} else {
80+
return "";
81+
}
82+
};
83+
84+
/**
85+
* 監聽 user.username
86+
* 監聽來源:整個物件。
87+
* 做法:需要搭配設定為深層監聽。
88+
*/
89+
watch(
90+
user, // 監聽整個物件
91+
(newVal) => {
92+
console.log(`#1 newVal ==>`, newVal);
93+
errors.value.username = checkName(newVal.username);
94+
},
95+
{
96+
deep: true, // 深層監聽
97+
}
98+
);
99+
100+
101+
/**
102+
* 監聽 user2.username
103+
* 監聽來源:物件內的某個純值屬性。
104+
* 做法:需要使用 getter 函式作為監聽來源的寫法。
105+
*/
106+
watch(
107+
() => user2.value.username, // 使用 getter 函式
108+
(newVal) => {
109+
console.log(`#2 newVal ==>`, newVal);
110+
errors2.value.username = checkName(newVal);
111+
},
112+
);
113+
114+
115+
/* 立即監聽 */
116+
watch(
117+
user3,
118+
(newVal) => {
119+
console.log(`#3 newVal ==>`, newVal);
120+
errors3.value.username = checkName(newVal.username);
121+
},
122+
{
123+
deep: true, // 深層監聽
124+
immediate: true, // 立即監聽
125+
}
126+
);
127+
</script>
128+
129+
<style lang="scss" scoped>
130+
.error {
131+
color: red;
132+
font-size: 0.8em;
133+
}
134+
</style>

src/views/day_jobs/DayJobs.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<RouterLink to="/dayjobs/day8">Day8</RouterLink>|
1313
<RouterLink to="/dayjobs/day9">Day9</RouterLink>|
1414
<RouterLink to="/dayjobs/day10">Day10</RouterLink>|
15-
<!-- <RouterLink to="/dayjobs/day11">Day11</RouterLink>| -->
15+
<RouterLink to="/dayjobs/day11">Day11</RouterLink>|
1616
<!-- <RouterLink to="/dayjobs/day12">Day12</RouterLink>| -->
1717
<!-- <RouterLink to="/dayjobs/day13">Day13</RouterLink>| -->
1818
<!-- <RouterLink to="/dayjobs/day14">Day14</RouterLink>| -->

0 commit comments

Comments
 (0)