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-01-09

初めに

最近フロントエンドに興味を持ち、Vue.jsについて勉強を始めました。
直近で学んだことのアウトプットとしての記事を投稿します。

いろいろなディレクティブ

ここからは v-for, v-bind, v-if, v-on, v-model について簡単な動作確認をしてみます。

v-for

  <div id="app">
    <ul>
      <li v-for="fluit in fluits">{{ fluit.name }}</li>
    </ul>
  </div>
var app = new Vue({
  el: "#app",
  data: {
    fluits: [
      {name: 'Peaches'},
      {name: 'Apples'},
      {name: 'Plums'}
    ]
  }
})
  • 表示内容

image.png

v-bind

  • input タグ属性書き換え
  <div id="app">
    <input type="button" v-bind:value="btnValue">
  </div>
var app = new Vue({
  el: '#app',
  data: {
    btnValue: 'ボタン'
  }
})
  • 表示内容

image.png

  • p タグ属性書き換え
  <div id="app">
    <p v-bind:title="message">John Smith</p>
  </div>
var app = new Vue({
  el: '#app',
  data: {
    message: 'Counry Prefecture 19xx~19yy'
  }
})
  • マウスホバー時

image.png

  • style 属性書き換え
  <div id="app">
    <p v-bind:style="styleObject">{{ message }}</p>
  </div>
var app = new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue!',
    styleObject: {
      margin: '100px',
      color: 'rgb(100, 50, 200)',
      fontSize: '100px',
      fontFamily: 'Playfair Display'
    }
  }
})

※font は https://fonts.google.com/ を使用

  • 表示内容

image.png

コンポーネント

  <div id="app">
    <greeting></greeting>
  </div>
Vue.component('greeting', {
  template: '<h1>Hello Vue!</h1>'
})
var app = new Vue({
  el: '#app'
})
  • 表示内容

image.png

v-for, v-bind, コンポーネント組み合わせ

  <fruits-list 
    v-for="favoriteFruit in favoriteFruitsList"
    v-bind:fruit="favoriteFruit">
  </fruits-list>
Vue.component('fruits-list', {
  props: ['fruit'],
  template: '<li>{{ fruit.name }}</li>'
})
var app = new Vue({
  el: "#app",
  data: {
    favoriteFruitsList: [
      {name: 'Peaches'},
      {name: 'Apples'},
      {name: 'Plums'}
    ]
  }
})
  • 表示内容

image.png

v-if

  <div id="app">
    <p v-if="display">Hello Vue!</p>
  </div>
var app = new Vue({
  el: "#app",
  data: {
    display: true
  }
})
  • コンソールログ

image.png

  • 表示内容

image.png

  • コンソールログにて操作(この場合画面表示なし)

image.png

v-on

  <div id="app">
    <p>{{ message }}</p>
    <button v-on:click="greetingCycle">greeting</button>
  </div>
var app = new Vue({
  el: '#app',
  data: {
    message: 'Good mornig!',
    count: 0,
    greetingList: [
      'Good mornig!',
      'Hello!',
      'Good evening!'
    ]
  },
  methods: {
    greetingCycle: function() {
      this.count = (this.count + 1) % 3;
      this.message = this.greetingList[this.count];
    }
  }
})
  • ボタン押下回数 0 回

image.png

  • ボタン押下回数 1 回

image.png

  • ボタン押下回数 2 回

image.png

  • ボタン押下回数 3 回

image.png

v-if, v-on 組み合わせ

  <div id="app">
    <input type="text" v-if="display">
    <input
      type="button"
      v-if="display"
      v-bind:value="submit"
      v-on:click="displayOnOff">
    <p v-if="completeMessage" v-bind:style="styles">{{ message }}</p>
  </div>
var app = new Vue({
  el: '#app',
  data: {
    message: 'お疲れ様でした!',
    submit: '回答する',
    display: true,
    completeMessage: false,
    styles: {
      color: 'rgb(255, 255, 255)',
      backgroundColor: 'blue',
      padding: '20px'
    }
  },
  methods: {
    displayOff: function() {
      this.display = false;
      this.completeMessage = true;
    }
  }
})
  • ボタン押下前

image.png

  • ボタン押下後

image.png

v-model

  <div id="app">
    <input v-model="message" type="text" size="50">
    <p :style="fonts">{{ message }}</p>
  </div>
var app = new Vue({
  el: '#app',
  data: {
    message: '',
    fonts: {
      fontFamily: 'Parisienne'
    }
  }
})

:stylev-bind:style の省略記法。
※font は https://fonts.google.com/ を使用

  • 表示内容

image.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?