今回はVue3の書き方を覚えるために、定番とされるカウント機能付きのメモアプリを作成してみました。聞いていた通り、Vue3はVue2とはやはり全然違う書き方になっていますね。
今回はhtmlファイルにcssもvueも直接埋め込んでいますので、下記1ファイルのみでアプリを確認できます。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>カウント付きメモアプリ</title>
<link rel="stylesheet" href="https://unpkg.com/ress/dist/ress.min.css">
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<style>
body {
background-color: #f5f5f5;
margin: 0;
}
.app-container {
max-width: 500px;
margin: 2rem auto;
padding: 1rem;
font-family: sans-serif;
background: white;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
textarea {
width: 100%;
padding: 10px;
box-sizing: border-box;
margin-bottom: 0.5rem;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical;
}
.counter {
text-align: right;
margin-bottom: 0.5rem;
color: #666;
}
button {
width: 100%;
padding: 10px;
background-color: #42b883;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-weight: bold;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
ul {
list-style: none;
padding: 0;
}
.memo-item {
background: #f9f9f9;
border: 1px solid #ddd;
padding: 10px;
margin-bottom: 10px;
border-radius: 4px;
}
.memo-text {
white-space: pre-wrap;
margin: 0 0 10px 0;
}
.memo-footer {
display: flex;
justify-content: space-between;
align-items: center;
font-size: 0.85rem;
color: #666;
}
.delete-btn {
width: auto;
padding: 3px 8px;
background-color: #ff4d4f;
}
</style>
</head>
<body>
<div id="app" class="app-container">
<h1>カウント付きメモアプリ</h1>
<div class="input-section">
<textarea v-model="memoText" id="" placeholder="メモを入力してください・・・" rows="4"></textarea>
<div class="counter">現在の文字数:<strong>{{charCount}}</strong> 文字</div>
<button @click="addMemo" :disabled="!memoText.trim()">メモを保存</button>
</div>
<div class="list-section">
<h2>保存されたメモ({{memoList.length}}件)</h2>
<p v-if="memoList.length === 0">保存されたメモはありません。</p>
<ul>
<li v-for="memo in memoList" :key="memo.id" class="memo-item">
<p class="memo-text">{{memo.text}}</p>
<div class="memo-footer">
<span> ({{memo.count}} 文字) </span>
<button @click="deleteMemo(memo.id)" class="delete-btn">削除</button>
</div>
</li>
</ul>
</div>
</div>
<script>
const { createApp, ref, computed } = Vue;
createApp({
setup() {
const memoText = ref('');
const memoList = ref([]);
const charCount = computed(() => memoText.value.length);
const addMemo = () => {
if(!memoText.value.trim()) return;
memoList.value.push({
id: Date.now(),
text: memoText.value,
count: memoText.value.length
});
memoText.value = ''; // 入力欄をクリア
};
const deleteMemo = (id) => {
memoList.value = memoList.value.filter(memo => memo.id !== id);
};
return {
memoText,
memoList,
charCount,
addMemo,
deleteMemo
};
}
}).mount('#app')
</script>
</body>
</html>
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
【▼今回のVue.js3コーディングの流れ】
1、まず最初にVueから使いたい関数一式を取り出す。
2、createApp関数を実行してアプリを作成する(アプリのコードは全てこの中に入れる)。
3、refでmemoText、memoListをリアクティブな変数として定義する。
4、charCount(文字数)を算出プロパティを使って定義する。
5、addMemo(メモ追加の関数)を定義する。
入力された文字・ID・文字数をオブジェクトにしてmemoList配列に追加する。
入力欄(memoText)を空にしてクリアする。
6、deleteMemo(メモ削除の関数)を定義する。
7、returnでhtml側で使いたい変数や関数を返す。
8、最後に.mount(‘#app’)でhtmlのappにアプリをマウント(取り付け)して起動する。
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
Vue2の基礎を学習した人がVue3の書き方を学習する際にまず作ると良いアプリとしてAIにおすすめされたのがこの「カウント付きメモアプリ」でしたのでとりあえず書いてみました。
確かにVue3のComposition APIの特徴がとてもよく出ているコードなので、Vue2の書き方と比較しながら見てみるとその違いがよく分かりますね。
個人的には、「new Vue関数の中にdata、computed、methodsプロパティを作ってそれぞれの機能別に書いていくVue2の書き方」よりも、
「createApp関数の中に各関数や変数の定義ベースで書いていき、最後にhtmlの#appに乗せて終わりというVue3の書き方」
のほうがシンプルで分かりやすくなったなという印象を持ちました。元祖Vanilla JSの形に幾分戻った感じがします。Vue.jsは良い方向へ進化してくれていますね。
それでは今回はこの辺で。
ご覧いただきありがとうございました。