Vue.jsでよく使う機能メモ。コピペで動作が確認できます。
v-cloakを使ってマスタッシュ構文を表示させない方法
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>V-CLOAK</title>
<style>
[v-cloak] {
display:none;
}
</style>
</head>
<body>
<div id="app" v-cloak>
{{ message }}
</div>
<script src="https://unpkg.com/vue@next"></script>
<script>
const AttributeBinding = {
data() {
return {
message: 'V-CLOCK SAMPLE'
}
}
}
Vue.createApp(AttributeBinding).mount('#app');
</script>
</body>
</html>
mountした後でdataを変更する方法
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SAMPLE</title>
</head>
<body>
<div id="app">
{{ message }}
</div>
<script src="https://unpkg.com/vue@next"></script>
<script>
const AttributeBinding = {
data() {
return {
message: 'AAA'
}
}
}
vm = Vue.createApp(AttributeBinding).mount('#app');
vm.message = "BBB";
</script>
</body>
</html>
dataに連想配列を使用する方法
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dictionary</title>
</head>
<body>
<div id="app">
{{ user.lastName }} {{ user.firstName }}
</div>
<script src="https://unpkg.com/vue@next"></script>
<script>
const AttributeBinding = {
data() {
return {
user: { firstName: 'Taro', lastName: 'Suzuki' }
}
}
}
Vue.createApp(AttributeBinding).mount('#app');
</script>
</body>
</html>
メソッドを作らずにイベントを直書きする方法
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Change Last Name</title>
</head>
<body>
<div id="app">
{{ user.lastName }} {{ user.firstName }}
<button type="button" @click="() => user.lastName='Tanaka'">Tanaka</button>
<button type="button" @click="(event) => user.lastName = event.target.innerHTML">Yamada</button>
</div>
<script src="https://unpkg.com/vue@next"></script>
<script>
const AttributeBinding = {
data() {
return {
user: { firstName: 'Taro', lastName: 'Suzuki' }
}
}
}
Vue.createApp(AttributeBinding).mount('#app');
</script>
</body>
</html>
コンポーネントの使用例
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Components</title>
</head>
<body>
<div id="app">
<ol>
<user-item v-for="item in list" :user="item" :key="item.id"></user-item>
</ol>
</div>
<script src="https://unpkg.com/vue@next"></script>
<script>
const CustomLi = {
props: ['setColor'],
template: `<li :style="{ color: setColor }">
<slot></slot>
</li>`,
}
const UserItem = {
props: ['user'],
template: `<custom-li :setColor="user.setColor">
{{ user.lastName }} {{ user.firstName }}
</custom-li>`,
components: {
'custom-li': CustomLi
}
}
const AttributeBinding = {
data() {
return {
list: [
{ id: 1, firstName: 'Taro', lastName: 'Suzuki', setColor : "red" },
{ id: 2, firstName: 'Hanako', lastName: 'Yamada', setColor : "blue" },
{ id: 3, firstName: 'Ichiro', lastName: 'Sato', setColor : "yellow" }
]
}
},
components: {
'user-item': UserItem
}
}
Vue.createApp(AttributeBinding).mount('#app');
</script>
</body>
</html>