Vue js component 親コンポーネント から子コンポーネント へデータの渡し方(props , slot)

参考動画

https://www.youtube.com/watch?v=uNR_WKLFh48&list=PLkQpCfbvj0lwds9oPsRRrFp1ZxTGyueqv&index=14

 

メモ

propsはdataを扱うのと似た感じで使える

slotを使うとタグの間に文字が入れられて視認性が良い

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>コンポーネントから子コンポーネント の練習</h1>
<div id="app">
<h2>propsの使い方</h2>
<paragragh message='hello'></paragragh>
<paragragh message='world'></paragragh>
<br>
<h2>slotの使い方</h2>
<paragragh2>hogehogehoge</paragragh2>
</div>


<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script>
Vue.component('paragragh',{
props:['message'],
'template':`<p>{{message}}</p>`
})
Vue.component('paragragh2',{
'template':`<p><slot></slot></p>`
})


new Vue({
el:'#app',
data:{}
})

 
</script>
</body>
</html>

 

結果

f:id:kirikko_Scondcube:20200731184255p:plain