相比 angular js react 来说,vue.js 是轻量化,容易上手,这对于普通公司来说 是节约成本的。新人老人都上手快,新人老人之间互相交流更容易。
做产品 一要可用 二要低成本。不一定要大而全 的 如angular js。 适用就好。
摘录例子 可看出 便捷之处
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="styles/demo.css" />
<script src="js/vue.min.js"></script>
</head>
<body>
<div id="app">
<fieldset>
<legend>
Create New Person
</legend>
<div class="form-group">
<label>Name:</label>
<input type="text" v-model="newPerson.name"/>
</div>
<div class="form-group">
<label>Age:</label>
<input type="text" v-model="newPerson.age"/>
</div>
<div class="form-group">
<label>Sex:</label>
<select v-model="newPerson.sex">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
<div class="form-group">
<label></label>
<button @click="createPerson">Create</button>
</div>
</fieldset>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Sex</th>
<th>Delete</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)">Delete</button></td>
</tr>
</tbody>
</table>
</div>
</body>
<script>
var vm = new Vue({
el: '#app',
data: {
newPerson: {
name: '2',
age: 0,
sex: 'Male'
},
people: [{
name: 'Jack',
age: 30,
sex: 'Male'
}, {
name: 'Bill',
age: 26,
sex: 'Male'
}, {
name: 'Tracy',
age: 22,
sex: 'Female'
}, {
name: 'Chris',
age: 36,
sex: 'Male'
}]
},
methods:{
createPerson: function(){
this.people.push(this.newPerson);
// 添加完newPerson对象后,重置newPerson对象
this.newPerson = {name: '', age: 0, sex: 'Male'}
},
deletePerson: function(index){
// 删一个数组元素
this.people.splice(index,1);
}
}
})
</script>
</html>
---------------------------------------服务端交互
<script src="https://cdn.jsdelivr.net/vue/latest/vue.js"></script>
<script type="text/javascript" src='http://cdnjsnet.b0.upaiyun.com/vue-resource/0.1.9/vue-resource.min.js'></script>
<div id='app'></div>
<script>
new Vue({
el: '#app',
ready: function() {
this.$http.get('test.json', function(data) {
this.$set('json', data);
}).error(function(data, status, request) {
console.log('fail' + status + "," + request);
})
},
data: {
}
});
</script>
GET请求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
JSONP请求
1 2 3 4 5 |
|
POST请求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
<!DOCTYPE html>
<html lang=
"en"
>
<head>
<meta charset=
"UTF-8"
>
<title>vue js</title>
<style>
.completed {
text-decoration: line-through;
}
.something {
color: red;
}
</style>
</head>
<body>
<div class=
"container"
>
<div id=
"app"
>
<task-app :list=
"tasks"
>
</task-app>
</div>
<template id=
"task-template"
>
<ul>
<li v-
for
=
"task in list"
>
{{ task.id }} | {{ task.author }} | {{ task.name }} | {{ task.price }}
</li>
</ul>
</template>
<script src=
"vue.js"
></script>
<script src=
"vue-resource.js"
></script>
<script>
Vue.component(
'task-app'
, {
//要应用的标签
template:
'#task-template'
,
//模板id
props: [
'list'
]
//请求的json
})
</script>
<script>
var
demo =
new
Vue({
el:
'#app'
,
data: {
tasks:
''
//为空,可以是null
},
ready:
function
() {
this
.getCustomers()
},
methods: {
getCustomers:
function
() {
this
.$http.get(
'resourse.json'
)
.then(
function
(response) {
//response传参,可以是任何值
this
.$set(
'tasks'
, response.data)
})
.
catch
(
function
(response) {
console.log(response)
})
}
}
})
</script>
</body>
</html>