如果我们想实时的显示我们todos的长度这个业务逻辑可以这样:

<h1>{{message}} ({{todos.length}})</h1>

直接展示todos的length属性,这种可读性不好我们希望以一个变量来展示todos的长度,这就可以使用我们的 computed,js代码:

        new Vue({
el:'#app',
data:{
message:'hello world',
todos:[
{id:1,title:"learn vuejs"},
],
newTodo:{id:null,title:""}//定义一个obj
},
computed:{
todoCount(){
return this.todos.length;
}
},
methods:{
addTodo(newTodo){//es6
this.todos.push(newTodo);//把新的obj添加在数组中,
this.newTodo = {id:null,title:""}//初始化newTodo
},
deleteTodo(index){
this.todos.splice(index,1);//删除下标为index的元素
}
}
});

页面中:

<h1>{{message}} ({{todoCount}})</h1>

这样也是可以的,这样主要用于我们在页面当中展示的一些数据可能需要一些处理之后才可以使用。 v-bind:class 设置我们的class,对于我们改变样式是非常有帮助的,比如我们要判断我们的todo item是否是一个完成的状态。

首先我们在我们的todos里面的newTodo中添加一个completed:false,然后使用我们的v-bind:class

<li class="list-group-item" v-for="(todo,index) in todos" v-bind:class="{'completed' : todo.completed}">

这里就是说如果我们的todo.completed为真就给我们的这个li添加一个class名“completed”,在后面添加一个按钮来控制是否完成。

<button class="btn btn-warning btn-xs pull-right" v-on:click="toggleCompletion(todo)">Complete</button>

当然还要把我们的方法写上,功能就是点击把completed变为相反的状态:

toggleCompletion(todo){
todo.completed = !todo.completed;
}

还可以把点击的时候将按钮本省的样式改变  也是用v-bind:class,用另一种形式来写  也就是我们的三目运算符:

<button class="btn btn-xs pull-right" v-on:click="toggleCompletion(todo)" v-bind:class="[todo.completed ? 'btn-success' : 'btn-danger']">{{todo.completed ? 'completed' : 'working'}}</button>

整个的页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>vue2.0</title>
<link rel="stylesheet" type="text/css" href="./css/bootstrap.min.css">
<style type="text/css">
.completed{
color: green;
font-style: italic;
}
</style>
</head>
<body>
<nav class="navbar navbar-default navbar-static-top">
<div class="container" id="app">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">welcome Vue 2.0</div>
<div class="panel-body">
<input type="text" name="" v-model="message" class="form-control">
<h1>{{message}} ({{todoCount}})</h1>
<ul class="list-group">
<li class="list-group-item" v-for="(todo,index) in todos" v-bind:class="{'completed' : todo.completed}">
{{todo.title}}
<button class="btn btn-warning btn-xs pull-right" v-on:click="deleteTodo(index)">Delete</button>
<button class="btn btn-xs pull-right" v-on:click="toggleCompletion(todo)" v-bind:class="[todo.completed ? 'btn-success' : 'btn-danger']">{{todo.completed ? 'completed' : 'working'}}</button>
</li>
</ul>
<form v-on:submit.prevent="addTodo(newTodo)">
<div class="form-group">
<input type="text" name="" class="form-gcontrol" placeholder="add a new todo" v-model="newTodo.title">
</div>
<div class="from-group">
<button class="btn btn-success" type="submit">add todo</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="js/vue.js"></script>
<script type="text/javascript">
new Vue({
el:'#app',
data:{
message:'hello world',
todos:[
{id:1,title:"learn vuejs",completed:false},
],
newTodo:{id:null,title:"",completed:false}//定义一个obj
},
computed:{
todoCount(){
return this.todos.length;
}
},
methods:{
addTodo(newTodo){//es6
this.todos.push(newTodo);//把新的obj添加在数组中,
this.newTodo = {id:null,title:"",completed:false}//初始化newTodo
},
deleteTodo(index){
this.todos.splice(index,1);//删除下标为index的元素
},
toggleCompletion(todo){
todo.completed = !todo.completed;
}
}
});
</script>
</body>
</html>

vue.js 2.0开发(2)的更多相关文章

  1. vue.js 2.0开发(3)

    组件化 Vue.component('todo-items',{ }); 定义组件,首先是标签的名字todo-items,然后里面还要配置一些选项,首先是我们的模板template,里面需要填入的内容 ...

  2. vue.js 2.0开发

    创建一个工程文件: css中引用的是bootstrap的css,js中就是vue,index页面: <!DOCTYPE html> <html> <head> &l ...

  3. vue.js 2.0开发(4)

    使用vue-cli,首先安装: npm install -g vue-cli 安装完了执行vue命令,会出现 vue init <template-name> <project-na ...

  4. 窥探Vue.js 2.0

    title: 窥探Vue.js2.0 date: 2016-09-27 10:22:34 tags: vue category: 技术总结 --- 窥探Vue.js2.0 令人兴奋的Vue.js 2. ...

  5. Vue.js 2.0 和 React、Augular

    Vue.js 2.0 和 React.Augular 引言 这个页面无疑是最难编写的,但也是非常重要的.或许你遇到了一些问题并且先前用其他的框架解决了.来这里的目的是看看Vue是否有更好的解决方案.那 ...

  6. 更轻更快的Vue.js 2.0与其他框架对比(转)

    更轻更快的Vue.js 2.0 崭露头角的JavaScript框架Vue.js 2.0版本已经发布,在狂热的JavaScript世界里带来了让人耳目一新的变化. Vue创建者尤雨溪称,Vue 2.0  ...

  7. 【转】Vue.js 2.0 快速上手精华梳理

    Vue.js 2.0 快速上手精华梳理 Sandy 发掘代码技巧:公众号:daimajiqiao 自从Vue2.0发布后,Vue就成了前端领域的热门话题,github也突破了三万的star,那么对于新 ...

  8. 还学的动吗? 盘点下Vue.js 3.0.0 那些让人激动的功能

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文出处:https://blog.bitsrc.io/vuejs-3-0-0-beta-features- ...

  9. Vue.js 3.0搭配.NET Core写一个牛B的文件上传组件

    在开发Web应用程序中,文件上传是经常用到的一个功能. 在Jquery时代,做上传功能,一般找jQuery插件就够了,很少有人去探究上传文件插件到底是怎么做的. 简单列一下我们要做的技术点和功能点 使 ...

随机推荐

  1. [转]Mathematical Induction --数学归纳法1

    Mathematical Induction Mathematical Induction is a special way of proving things. It has only 2 step ...

  2. a标签不跳转

    <a href="javascript://">父级菜单</a> 结果是这种写法在一些浏览器下不能到达预期效果(无跳转),我没有花时间把这种写法在主流浏览器 ...

  3. 部署点评Cat监控项目(转)

    原文地址:http://www.bubuko.com/infodetail-986338.html 在项目中监控代码运行的状况,可以采用点评的Cat项目来监控整个项目,但是按照官方的文档来部署cat, ...

  4. HashMap归档-超越昨天的自己系列

    java HashMap 读一下源码,一个数组存储数据: transient Entry[] table; 内部存key和value的内部类: static class Entry<K,V> ...

  5. 6.Counting Point Mutations

    Problem Figure 2. The Hamming distance between these two strings is 7. Mismatched symbols are colore ...

  6. Navicat premium工具常用快捷键

    Navicat premium是一款数据库管理工具,它可以以单一程式同时连线到MySQL.PostgreSQL. Oracle .SQL Server 及 SQLite 资料库,让管理不同类型的资料库 ...

  7. POSTGRES与JDBC对照

    POSTGRES与JDBC对照 未经验证,仅供参考.

  8. javascript中escape()、unescape()、encodeURI()、encodeURIComponent()、decodeURI()、decodeURIComponent()比较

    这些URI方法encodeURI.encodeURIComponent().decodeURI().decodeURIComponent()代替了BOM的escape()和unescape()方法.U ...

  9. Android驱动开发前的准备(三)

    Git使用入门 3.1安装Git 3.2查看Git文档 3.3源代码的提交与获取 3.1安装Git # apt-get install git # apt-get install git-doc gi ...

  10. include/linux/tasks.h

    #ifndef _LINUX_TASKS_H#define _LINUX_TASKS_H /* * This is the maximum nr of tasks - change it if you ...