Vue.js系列一

來源:互聯網
上載者:User

標籤:輸入   index   ges   庫檔案   https   效果   onload   bar   images   

一、什麼是Vue.js

Vue.js是一個構建資料驅動的 web 介面的漸進式架構。Vue.js 的目標是通過儘可能簡單的 API 實現響應的資料繫結和組合的視圖組件。

二、入門

引入類庫檔案:<script src="https://unpkg.com/vue/dist/vue.js"></script>

第一個demo:

html:

<div id="app">       <p>{{ message }}</p></div>

js:

onload = function () {    new Vue({        el:"#app",        data:{            message:"hello world"        }    });}

結果: hello world

三、使用

1、雙向繫結

html:

<div id="app">       <p>{{ message }}</p>       <input v-model="message"></div> 

js同上。

效果:當文字框裡面的東西改變時,p標籤的內容也改變。

2、列表渲染

html:

    <ul id="example-1">        <li v-for="item in items">             {{item.message}}        </li>    </ul>

js:

onload = function () {    var example1 = new Vue({        el: ‘#example-1‘,        data: {            items: [                { message: ‘Foo‘ },                { message: ‘Bar‘ }            ]        }    })}

結果:

  • Foo
  • Bar

3、處理使用者輸入

html:

<div id="app">        <p>{{message}}</p>        <button v-on:click="reverseMessage">翻轉</button>    </div>

js:

onload = function () {    new Vue({        el: "#app",        data: {            message: "hello world"        },        methods: {            reverseMessage: function () {                this.message=this.message.split(‘‘).reverse().join(‘‘)            }        }    });}

結果:當點擊按鈕的時候字母就可以翻轉

4、以上綜合

html:

    <div id="app">        <input v-model="newTodo" v-on:keyup.enter="addTodo" />        <ul>            <li v-for="todo in todos">                <span>{{todo.text}}</span>                <button v-on:click="removeTodo($index)">X</button>            </li>        </ul>    </div>

js:

onload = function () {    new Vue({        el: "#app",        data: {            newTodo: "",            todos: [                { text: "add some todos" }            ]        },        methods: {            addTodo: function () {                var text = this.newTodo.trim()                if (text) {                    this.todos.push({ text: text })                    this.newTodo = ""                }            },            removeTodo: function (index) {                this.todos.splice(index, 1)//splice(刪除的位置,刪除的數量)            }        }    });}

效果:

 

Vue.js系列一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.