0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

コンポーネントについて

Last updated at Posted at 2021-05-08

コンポーネントとは再利用できるvueのインスタンスである。

間違った使い方から

<script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.js"></script>

<div id="app">
</div>

<div id="app">
</div>
new Vue({
 el: '#app',
 data: {
   number: 12
 },
 template: '<p>いいね{{number}}</p>'
})

20210508-084738.png

こちらの意図としては同じdivタグを2つ用意して「いいね12」が2連続表示されるかと思っていたが、その意図には反して最初に示したものと同じものは無視するというvuejsの仕様で無視されている。

ここでコンポーネントの設定をして2回連続表示します。

Vue component('コンポーネントの名前',{コンポーネントの情報=データなど})

(1)vue.componentを設定します。
(2)vueファイルにコンポーネント名のタグを設置します。
*今回は
(3)Dataがある場合は関数を使ってreturnとオブジェクトで表現する。
今回はプロパティがnumberで値が12です。
⇨data: function(){return {number: 12}}

<script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.js"></script>

<div id="app">
<my-component></my-component>
</div>

<div id="app">
</div>
Vue.component('my-component',{
 data: function(){
   return {
     number: 12
   }
 },
 template: '<p>いいね({{number}})</p>'
})

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

20210508-092117.png

<div id="app">
<my-component></my-component>
<my-component></my-component>
<my-component></my-component>
</div>

繰り返し処理

20210508-092348.png

うまくいきました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?