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.

【vue.js】{{  }}二重中括弧

Last updated at Posted at 2021-05-04
<body>
  <script src="vue.js"></script>
   <div id="abc">
    <p>{{message}}</p>
   </div>
  <script>

  new Vue({
   el: '#abc',
    data: {
     message: 'HelloWorld!'
          },
         })
   </script>
</body>

{{}}二重中括弧はnew Vueでコードされたdataの中の同じキー[message]を探してその値[HelloWorld]を代入し表示してくれる。

20210504-104708.png

(発展)
{{}}の中身はRuby on Railsの式展開#{}のように変数や構文が使えます。

・データの中身を5に変えて

data: { number: 5}
<p>{{number}}</p>

20210504-111122.png

・その数を使って計算させてみる

<p>{{number + 3}}</p>

20210504-111439.png

・関数を使ってメソッドを作って表示
同じ処理を定義し何度も使い回しができる形にしたもの。

関数とは

<script>
  function 関数名(){
  処理
  }
</script>

無名関数とは関数名なしのもの。

<script>
  function(){
  処理
  }
</script>

今回は

<body>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.js"></script>
<div id="app">
<p>{{sayYes()}}</p>
</div>
<script>
  new Vue({
    el: '#app',
      // data: { number: 5}
   methods: {
    sayYes: function() {
      return 'Yes';
    }
  }
})
</script>
</body>

20210504-113216.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?