<!DOCTYPE html> <html> <head> <title>My first Vue app</title> <script src="https://unpkg.com/vue"></script> </head> <body> <div id="app"> {{ message }} </div> <div id="app-2"> <span v-bind:title="message"> 鼠標懸停幾秒鍾查看此處動態綁定的提示信息! </span> </div> <div id="app-3"> <p v-if="seen">現在你看到我了</p> </div> <div id="app-4"> <ol> <li v-for="todo in todos"> <!--注:Angular *ngFor="let todo of todos;let i = index" 這種循環遍歷方式--> {{ todo.text }} </li> </ol> </div> <div id="app-5"> <p>{{ message }}</p> <button v-on:click="reverseMessage">反轉消息</button> <!--注:Angular 定義一個function是用(click)="reverseMessage" 這種形式--> </div> <div id="app-6"> <p>{{ message }}</p> <input v-model="message"><!--注:Angular 雙向數據綁定[(ngModel)]='message'--> </div> <div id="app-7"> <ol> <!-- 現在我們為每個 todo-item 提供 todo 對象 todo 對象是變量,即其內容可以是動態的。 我們也需要為每個組件提供一個“key”,稍后再 作詳細解釋。 --> <todo-item v-for="item in groceryList" v-bind:todo="item" v-bind:key="item.id" ></todo-item> </ol> </div> <!--JS code start--> <script> //example1 var app = new Vue({ el: '#app', data: { message: 'Hello Vue!' } }) //example2 var app2 = new Vue({ el: '#app-2', data: { message: '頁面加載於 ' + new Date().toLocaleString() } }) app2.message = '新消息' //example3 var app2 = new Vue({ el: '#app-3', data: { seen: true } }) //example4 創建數組對象 循環遍歷使用過 var app4 = new Vue({ el: '#app-4', data: { todos: [ { text: '學習 JavaScript' }, { text: '學習 Vue' }, { text: '整個牛項目' } ] } }) //example5 定義一個function var app5 = new Vue({ el: '#app-5', data: { message: 'Hello Vue.js!' }, methods: { /** Angular定義function是直接寫reverseMessage()就可以調用 */ reverseMessage: function () { this.message = this.message.split('').reverse().join('') } } }) //example6 雙向數據綁定 var app6 = new Vue({ el: '#app-6', data: { message: 'Hello Vue!' } }) //example7 定義名為 todo-item 的新組件 Vue.component('todo-item', { props: ['todo'], template: '<li>{{ todo.text }}</li>' }) var app7 = new Vue({ el: '#app-7', data: { groceryList: [ { id: 0, text: '蔬菜' }, { id: 1, text: '奶酪' }, { id: 2, text: '隨便其它什么人吃的東西' } ] } }) </script> </body> </html>