Skip to content

Vue.js チートシート

Vue は、ユーザー インターフェイスを構築するための進化的な JavaScript フレームワークです。他のモノリシックなフレームワークとは異なり、Vue はボトムアップで段階的に採用できるように設計されています。Vue のコア ライブラリはビュー レイヤーのみに焦点を当てているため、サードパーティのライブラリや既存のプロジェクトとの統合が容易です。

リスト

<li v-for="todo in todos">
  {{ todo.text }}
  {{ $index }}
</li>

イベント

<button v-on:click='submit'>実行</button>

コンポーネント

new Vue({
  components: { app: App }
})

API インターフェース

Vue.extend({ ... })        // コンポーネントの作成
Vue.nextTick(() => {...})

Vue.set(object, key, val)  // リアクティブ
Vue.delete(object, key)

Vue.directive('my-dir', { bind, update, unbind })
// <div v-my-dir='...'></div>

Vue.elementDirective('my-dir', { bind, update, unbind })
// <my-dir>...</my-dir>

Vue.component('my-component', Vue.extend({ .. }))

Vue.partial('my-partial', '<div>こんにちは {{msg}}</div>')
// <partial name='my-partial'></partial>
new Vue({
  data: { ... }
  props: ['size'],
  props: { size: Number },
  computed: { fullname() { return this.name + ' ' + this.lastName } },
  methods: { go() { ... } },
  watch: { a (val, oldVal) { ... } },
  el: '#foo',
  template: '...',
  replace: true, // 要素を置き換える (デフォルトは true)

  // ライフサイクル
  created () {},
  beforeCompile () {},
  compiled () {},
  ready () {}, // $el が初めて挿入されたとき
  attached () {},
  detached () {},
  beforeDestroy () {},
  destroyed () {},

  // オプション
  directives: {},
  elementDirectives: {},
  filters: {},
  components: {},
  transitions: {},
  partials: {}
})

Vue テンプレート

vueify を参照

// app.vue
<template>
  <h1 class="red">{{msg}}</h1>
</template>
 
<script>
  module.exports = {
    data () {
      return {
        msg: 'ハローワールド!'
      }
    }
  }
</script> 

および

<template lang='jade'>
h1(class='red') {{msg}}
</template>