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 構文

0
Last updated at Posted at 2020-06-18

Vue.jsスクリプトタグの記載

<body>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.11"></script>    
</body>

id属性とバインド

id="app"にVueインスタンスのel:'#app'がバインド
{{ message }}へ data:{message:'Hello'}が表示される。

<body>
    <div id="app">
        {{ message }}
    </div>
    <script>
        new Vue({
            el:'#app',
            data:{
                message:'Hello'
            }
        })
    
    </script>
</body>

v-forでデータをループする

    <div id="app">
        <ul>
            <li v-for="person in persons">
                {{ person.name }}{{ person.age }}
            </li>
        </ul>

    </div>
    <script>
        new Vue({
            el:'#app',
            data: {
                persons:[{
                    name:'田中',
                    age:25,
                },
                {
                    name:'中田',
                    age:28,
                },
                {
                    name:'山田',
                    age:32,
                }]
            }
        })
    
    </script>

イベント発生時の関数実行する

    <div id="app">
        <div>{{ counter }}</div>
        <div>
            <button v-on:click="increment">ボタン</button>  //v-onを@clickで省略可
        </div>
    </div>
    <script>
        new Vue({
            el:'#app',
            data: {
                counter: 0
            },
            methods: {
                increment: function(){
                    this.counter++;  //thisはVueインスタンスの中
            }
        })

v-bindで動的に変化させる(1)

ボタンで動的にリンク先を変更する

    <div id="app">
        <a v-bind:href="url">リンクはこちら</a>
        <div>
            <button @click="yahoo">yahoo</button>
            <button @click="google">google</button>
        </div>
    </div>
    <script>
        new Vue({
            el:'#app',
            data: {
                url: ''
            },
            methods: {
                yahoo: function(){
                    this.url= 'https://www.yahoo.co.jp/';
                },
                google: function(){
                    this.url= 'https://www.google.com/?hl=ja';
                }
            }
        })
    </script>

v-bindで動的に変化させる(2)

ボタンで動的にclassを変更させる

    <style>
        .warning{
            background-color: red;
        }
    </style>
<body>
    <div id="app">
        <a v-bind:class="{ warning: isActive }">こちら</a>
        <a v-bind:class="styleObj">こちら</a>
        <div>
            <button @click="switch">カラー変更</button>
        </div>
    </div>
    <script>
        new Vue({
            el:'#app',
            data: {
                isActive: false,
                styleObj: {
                    warning: false
                }
            },
            methods: {
                switch: function(){
                    this.isActive = !this.isActive;
                    this.styleObj.warning = !this.styleObj.warning;
                }
            }
        })
    </script>

v-ifで条件付きレンダリング

    <div id="app">
        <input v-if="formtype === 'user'" type="text" placeholder="ユーザー">
        <input v-else-if="formtype === 'password'" type="text" placeholder="パスワード">
        <input v-else type="text" placeholder="その他">
        <button @click="chenge">切り替え</button>
    </div>
    <script>
        new Vue({
            el:'#app',
            data: {
                formtype: 'user',
            },
            methods: {
                chenge: function(){
                    this.formtype = !this.formtype;
                }
            }
        })
    </script>

v-showで条件付きレンダリング

    <div id="app">
        <input v-show="formtype === 'user'" type="text" placeholder="ユーザー">
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.11"></script>
    <script>
        new Vue({
            el:'#app',
            data: {
                formtype: 'user',
            },
        })
    </script>

v-ifとv-showの使い分け

v-ifは処理が重くなる。v-showは最初の処理は重いが、再描画時に軽くなる。
条件実行時、変更することがほとんどない場合はv-if。
頻繁に切り替える場合はv-show。

入力フォームバインディング

    <div id="app">
        <input v-model="user" type="text">   //textのバインディング
        <div>{{ user }}</div>
        <textarea v-model="etc"></textarea>   //textareaのバインディング
        <pre>{{ etc }}</pre>
        <input type="checkbox" v-model="agree" value="音楽">音楽<br>   //checkboxのバインディング
        <input type="checkbox" v-model="agree" value="スポーツ">スポーツ<br>
        <input type="checkbox" v-model="agree" value="映画">映画<br>
        <div>{{ agree }}</div>
        <input type="radio" v-model="hobby" value="音楽">音楽<br>   //radioのバンディング
        <input type="radio" v-model="hobby" value="スポーツ">スポーツ<br>
        <input type="radio" v-model="hobby" value="映画">映画<br>
        <div>{{ hobby }}</div>
        <select v-model="selected">   //selectのバインディング
            <option v-for="friend in friends" :value="friend.name">{{ friend.name }}</option>
        </select>
        <div>{{ selected }}</div>
    </div>
    <script>
        new Vue({
            el:'#app',
            data: {
                user: '',
                etc: '',
                agree: [],
                hobby: '',
                selected: '',
                friends: [
                    {
                        id: 1,
                        name: '吉田'
                    },
                    {
                        id: 2,
                        name: '川口'
                    },
                    {
                        id: 3,
                        name: '木村'
                    },
                ],
            }
        })
    </script>

props

propsを書き換える

変更前

 <static-info
      class="mb-4"
      :url="localePath('/flow')"
      :text="$t('自分や家族の症状に不安や心配があればまずは電話相談をどうぞ')"
      :btn-text="$t('相談の手順を見る')"
 />

変更後

<static-info class="mb-4" v-bind="staticItem" />

 const staticItem = (url, text, btnText) => {
      return {
        url: localePath('/flow'),
        text: this.$t('自分や家族の症状に不安や心配があればまずは電話相談をどうぞ'),
        btnText: this.$t('相談の手順を見る')
      }
    }
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?