或者使用CDN <script src="https://unpkg.com/vue/dist/vue.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="vue.js"></script>
</head>
<body>
<div id="app">
todo list
<ul>
<li v-for="todo in todos">{{todo.title}}</li>
</ul>
</div>
<script>
new Vue({
el:'#app',
data:{
message:'hello world',
todos:[
{id:1,title:'my to do lsit'},
{id:2,title:'my to do lsit2'}
]
}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="vue.js"></script>
<style>
.completed{
color:green;
text-decoration:line-through;
}
.btn-danger{
background:yellow;
}
.btn-success{
background:green;
}
</style>
</head>
<body>
<div id="app">
<h1>my todo list {{todoCount}}</h1>
<ul>
<li v-for="(todo,index) in todos" v-bind:class="{'completed':todo.completed}">{{todo.title}} <button v-on:click="toggleCompletion(todo)" v-bind:class="[todo.completed? 'btn-danger':'btn-success']">{{todo.completed? 'undo':'complete'}}</button> <button v-on:click="deleteTodo(index)">delete</button></li>
</ul>
<form v-on:submit.prevent="addTodo(newTodo)">
<input type="text" v-model="newTodo.title">
<button type="submit">add</button>
</form>
</div>
<script>
new Vue({
el:'#app',
data:{
message:'hello world',
todos:[
{id:1,title:'my to do lsit',completed:false},
{id:2,title:'my to do lsit2',completed:true}
],
newTodo:{id:null,title:''}
},
computed:{
todoCount(){
return this.todos.length;
}
},
methods:{
addTodo(newTodo2){
this.todos.push(newTodo2);
this.newTodo={id:null,title:'',completed:false}
},
deleteTodo(index){
this.todos.splice(index,1);
},
toggleCompletion(todo2){
todo2.completed=!todo2.completed;
}
}
});
</script>
</body>
</html>
第三节 组件的使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="vue.js"></script>
<style>
.completed{
color:green;
text-decoration:line-through;
}
.btn-danger{
background:yellow;
}
.btn-success{
background:green;
}
</style>
</head>
<body>
<div id="app">
<h1>my todo list {{todoCount}}</h1>
<todo-items :todos="todos"></todo-items>
<todo-form :todos="todos"></todo-form>
</div>
<script type="text/x-template" id="todo-items-template">
<ul>
<li v-for="(todo,index) in todos" v-bind:class="{'completed':todo.completed}">{{todo.title}} <button v-on:click="toggleCompletion(todo)" v-bind:class="[todo.completed? 'btn-danger':'btn-success']">{{todo.completed? 'undo':'complete'}}</button> <button v-on:click="deleteTodo(index)">delete</button></li>
</ul>
</script>
<script type="text/x-template" id="todo-form-template">
<form v-on:submit.prevent="addTodo(newTodo)">
<input type="text" v-model="newTodo.title">
<button type="submit">add</button>
</form>
</script>
<script>
Vue.component('todo-items',{
template:'#todo-items-template',
props:['todos'],
methods:{
deleteTodo(index){
this.todos.splice(index,1);
},
toggleCompletion(todo2){
todo2.completed=!todo2.completed;
}
}
});
Vue.component('todo-form',{
template:'#todo-form-template',
props:['todos'],
data(){
return {
newTodo:{id:null,title:''}
}
},
methods:{
addTodo(newTodo){
this.todos.push(newTodo);
this.newTodo={id:null,title:'',completed:false}
}
}
});
new Vue({
el:'#app',
data:{
message:'hello world',
todos:[
{id:1,title:'my to do lsit',completed:false},
{id:2,title:'my to do lsit2',completed:true}
],
},
computed:{
todoCount(){
return this.todos.length;
}
}
});
</script>
</body>
</html>
github地址 https://github.com/vuejs/vue-cli
根据提示 执行命令行
npm install -g vue-cli
然后在适当的目录下执行
vue init webpack vuejs-2.0-cli
如果你发现命令过程中遇到了错误,一时半会儿解决不了,那么就放弃命令下载吧
直接 下载文件包呗
将下载的文件解压缩移动到你合适的目录下
进入该项目目录 执行命令
npm install