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 5 years have passed since last update.

Vue.jsのテンプレート構文について

Posted at

⬇️HTMLっぽいこれがテンプレート構文です

<div id="app">
  <p>{{message}}</p>
</div>

これを、Vue.jsがいろんな処理をして、最終的にHTMLとして出力し、ブラウザがレンダリングしている!

  • マスタッシュタグの中に、messageとかsayHiとかのmain.jsのプロパティを記述します
App.vue
<div id="app">
  <p>{{message}}</p>
 <!--   マスタッシュタグの中身はJavaScriptをそのままかける -->
  <p>{{number + 4 }}</p>
  <p>{{sayHi()}}</p>
 <!-- マスタッシュタグ内に プロパティを記述する -->
</div>
main.js
new Vue({
	//elはクラス,id,divとかも取れる
	el:'#app',
	data:{
		message:'Hello Wold!',
    number:3
  },
  methods:{
		sayHi:function(){
			return 'Hi';
    }  
  }
})

methodsからdataにアクセスするには

インスタンス内のmethodsからdataにアクセスするには、以下のように書きます。

main.js
new Vue({
	//elはクラス,id,divとかも取れる
	el:'#app',
	data:{
		message:'Hello Wold!',
    number:3
  },
  methods:{
		sayHi:function(){
    //同じインスタンス内のmessageを表示させたい時、
			return this.message;
    }  
  }
})

thisをつけてプラパティであるmessage書くことで、
その値のHello World!が表示されます。

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?