🖥

Vue 初心者 - フォームの input の入力値をHTMLにリアルタイム反映する ( 同じComponentでのデータバインド ) (

に公開

vue cli で環境構築したプロジェクト前提

NOTE

  • メイン処理をする App.vue なのに、なぜexportするのか?どこにexportされるのか? Vue のより外側のどこかに伝わっているのだろうか。
  • 同じ component に対して働くのに export なのが、なんとも不思議な感じがするのが、きっとこういう流儀なのだろう。
  • data に書いても props で書いても、とりあえず同じように働くようだ。この作用の違いはまた調べたい。

参考: Vue.js を vue-cli を使ってシンプルにはじめてみる - Qiita

src/App.vue

<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <div>
      <input type="text" v-model="msg">
    </div>
    <h1>{{ msg }}</h1>
  </div>
</template>

<script>

export default {
  name: 'App',
  props: {
    msg: {
      type: String,
      default: 'Ya!',
    }
  }

  // data に書いても動く
  //
  // data () {
  //   return {
  //     msg: 'Ya!'
  //   }
  // }
}
</script>


<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

動作例

image

image

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/3100

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2020-04-30

Discussion