ch1-vuejs基礎入門

來源:互聯網
上載者:User

標籤:執行方法   應用   基礎   table   var   else   put   特性   pre   

1 hello world
引入vue.min.js 代碼: ----2.0+版本
<div id="test">
{{str}}
</div>
<script>
var app = new Vue({
el: ‘#test‘,
data: {
str: ‘hello world vuejs‘
}
});
</script>
2 v-bind 指令(v-)
v-bind:title=‘str‘
綁定到title,title顯示的時候就會觸發

3 條件與迴圈
條件 v-if="運算式的結果" 結果為真顯示否則隱藏
迴圈 v-for 同for() 資料來源為data的值

4 事件監聽 v-on 代碼展示
<div id="test5">
<p>{{str}}</p>
<button v-on:click="reverseStr">逆轉顯示</button>
</div>

var app5 = new Vue({
el: ‘#test5‘,
data: {
str: ‘hello world Vue.js‘
},
methods: {
reverseStr: function () {
this.str = this.str.split(‘‘).reverse().join(‘‘);
}
}
});

5 v-model 使表單輸入和應用狀態間雙向繫結 同ng-model
<div id="test6">
<p>{{str}}</p>
<input type="text" v-model="str">
</div>
var app6 = new Vue({
el: ‘#test6‘,
data: {
str: ‘兩隻老虎跑得快‘
}
});

6 vue.js內建的指令
v-if 條件渲染指令
v-show 條件渲染指令 指令的元素始終會被渲染到html,這是簡單的為元素設定style屬性
v-else
可以為v-if或v-show添加一個‘else塊‘,必須跟在其後面
v-else的元素是否顯示取決於前面是v-if還是v-show v-if為true v-else不會渲染到dom v-show為true或false v-else都不會渲染到html
v-for 遍曆數組 xx in xx
v-bind 文法:v-bind:argument=‘expression‘
argument表示html元素的特性如(attribute) 或 v-bind:class 可以高亮當前頁 可縮寫為:一個冒號
v-on 文法與v-bind類似 監聽dom事件
調用方法:1 綁定一個方法名(讓事件指向方法的引用)2 使用內聯語句(執行方法) 可縮寫為:@符號

7 應用組件化 --用小型,自包含和可以複用的組件構建大型應用
<div id="test">
<my-component></my-component>
</div>
<script>
Vue.component(‘my-component‘,{
template: ‘<h1>原來是個h1</h1>‘
});
var app = new Vue({
el: ‘#test‘,
})
</script>

<div id="test">
<my-component v-for="item in groceryList" :todo="item"></my-component>
</div>
<script>
Vue.component(‘my-component‘,{
//組件現在接收一個 ‘prop‘類屬於一個自訂屬性屬性名稱為todo
props: [‘todo‘],
template: ‘<h2>{{todo.text}}</h2>‘
});
var app = new Vue({
el: ‘#test‘,
data: {
groceryList: [
{ text: ‘蔬菜‘ },
{ text: ‘乳酪‘ },
{ text: ‘隨便其他什麼人吃的東西‘ }
]
}
})
</script>

8 小案例---由於 $index 問題 案例使用 ---vue.js 1.0.5

  
<div id="app">

<fieldset>
<legend>
添加新使用者
</legend>
<div class="form-group">
<label>姓名:</label>
<input type="text" v-model="newPerson.name"/>
</div>
<div class="form-group">
<label>年齡:</label>
<input type="text" v-model="newPerson.age"/>
</div>
<div class="form-group">
<label>性別:</label>
<select v-model="newPerson.sex">
<option value="男" selected = "selected">男</option>
<option value="女">女</option>
</select>
</div>
<div class="form-group">
<label></label>
<button @click="createPerson">添加</button>
</div>
</fieldset>
<table class="table table-bordered">
<thead>
<tr>
<th>姓名</th>
<th>年齡</th>
<th>性別</th>
<th>刪除</th>
</tr>
</thead>
<tbody>
<tr v-for="person in people">
<td>{{ person.name }}</td>
<td>{{ person.age }}</td>
<td>{{ person.sex }}</td>
<td :class="‘text-center‘"><button @click="deletePerson($index)">刪除</button></td>
</tr>
</tbody>
</table>
</div>

<script>
var app = new Vue({
el: ‘#app‘,
data: {
newPerson: {
name: ‘‘,
age: 0,
sex: ‘男‘
},
people: [{
name: ‘張三‘,
age: 30,
sex: ‘男‘
}, {
name: ‘李四‘,
age: 26,
sex: ‘男‘
}, {
name: ‘王五‘,
age: 22,
sex: ‘女‘
}, {
name: ‘趙六‘,
age: 36,
sex: ‘男‘
}]
},
methods: {
createPerson: function () {
this.people.push(this.newPerson);
//添加完newPerson對象後,重設newPerson對象
this.newPerson = {name:‘‘,age:‘‘,sex:‘男‘}
},
deletePerson: function (index) { //實參$index在2.0+版本無此功能
this.people.splice(index,1);
}
}
})
</script>

ch1-vuejs基礎入門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.