Vue筆記

Makedown語法
VueIT幫邦忙

引入vue

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>

Vue常見基本指令Vue directives

  1. v-bind:綁定html屬性 前往
  2. v-model:雙向數據綁定 前往
  3. v-for:循環渲染 前往
  4. v-if:條件渲染 前往
  5. v-on:事件綁定 前往

Vue教學

Data&methods認識

Vue架構

    Vue.component('my-component', {
        template: '<p>這是自定義組件!</p>'
    });

    new Vue({
        el:'#app', 
        data:{      
            message:'Hello,vue!', 
            modeltest:'雙向綁定測試',
            url:'https://www.google.com.tw',
            items: [
                { id: 1, text: '項目 1',price:7000, },
                { id: 2, text: '項目 2',price:8000,},
                { id: 3, text: '項目 3',price:9000, }
            ],
        },
        methods:{
            changeMessage() {
                this.message = '你好,Vue!';
            },
        },
        computed:{
            final_price(){
                return this.items.price*0.9;
            },
        },
    });

架構解釋

程式 解釋 備註
el Vue 實例的掛載點div上要掛id #app 掛載的id可以自己決定
data data 內是可以用來返回一個參數或是可以定義items等等 data內有許多種用法可參考下方
methods methods內可以定義多個用途如架構內的changeMessage() methods定義的東西不一定要有回傳值
computed 用來計算用的函釋 一定要有回傳值
component 自定義的組件 使用時要<my-component></my-component>

架構內自訂內容解釋

程式 解釋 備註
message 定義原始顯示的數據 寫在data內可以自訂內容
modeltest 雙向綁定測試 寫在data內可以自訂內容
url 定義連結 寫在data內可以自訂內容
items 清單 寫在data內可以自訂內容使用 v-for="item in items"可以回傳內容
changeMessage() 用在按鈕上可以回傳message內容
final_price() 回傳輸入的數值打九折 輸入框要使用v-model綁定

v-bind

    <p v-bind:title="message">將滑鼠放在我上面看見message定義的訊息</p>
    <p :title="message">將滑鼠放在我上面看見message定義的訊息</p>

Vue語法

data: {
        message: 'Hello, Vue!', // 定義原始顯示數據
}

使用結果

v-model

    <input v-model="modeltest" placeholder="輸入訊息">
    <p>輸入的訊息:{{ modeltest }}</p>

Vue語法

data: {
        modeltest:'雙向綁定',
},

使用結果

v-for

    <ul v-for="item in items" :key="item.id">
        <li>項目:{{ item.text }}</li>
        <li>價錢:{{ item.price }}</li>
        // 使用{{item.text}}可以取的item內的數值
    </ul>

Vue語法

data: {
    items: [
        { id: 1, text: '項目 1',price:7000, },
        { id: 2, text: '項目 2',price:8000,},
        { id: 3, text: '項目 3',price:9000, }
    ],
},

使用結果

v-if

    <p v-if="isLoggedIn">歡迎回來Vue!</p>
    <p v-else>請點擊登入。</p>
    <button @click="toggleLoginStatus">登入</button>

Vue語法

    data: {
        isLoggedIn: false,
    },
    

    methods:{
        toggleLoginStatus() {
            this.isLoggedIn = !this.isLoggedIn;
        }
    }
    

使用結果

v-if進階

  <ul>
    <li v-for="item in items" v-if="item.isVisible">{{ item.name }}</li>
  </ul>
  <button @click="toggleitem">顯示</button>

Vue語法

    data: {
        items: [
            { id: 1, text: '項目 1',price:7000,name:'阿哈哈1',isVisible: true},
            { id: 2, text: '項目 2',price:8000,name:'阿哈哈2',isVisible: true},
            { id: 3, text: '項目 3',price:9000,name:'阿哈哈3',isVisible: true}
          ],    
    },
    

    methods:{
        toggleitem(){
            this.items.forEach(item => {
                item.isVisible = !item.isVisible;
            });
    }
    

使用結果

v-on

    <button @click="changeMessage">v-on事件綁定的簡寫</button>
    <button v-on:click="changeMessage">v-on事件綁定的一般寫法</button>

Vue語法

    data: {
      message:'Hello,Vue!',
    },
    

    methods:{
        changeMessage() {
            this.message = '你好,Vue!'; // 改變p顯示的數據
        },
    }
    

使用結果