Rails5.2+Vue.js完成Lists(curd)

注意: Edit/update使用SPA(single-page Application单页面程序)的方法完成。点击文字出现一个输入框和按钮。

我的git: https://github.com/chentianwei411/vue-lists

app/javascript/pack/application.js

lists.vue

添加了方法

  • 在new Vue创建实例的时候添加了hook created方法,钩子方法调用了fetchLists方法用于得到数据:

    • fetchLists方法,其实对应(Controller#get)
  • addLine     (Controller#create)
  • changeLine方法, 用于配合v-show的显示/隐藏功能
  • deleteLine:  (Controller#destroy)
  • updateLine  (Congroller#update)

addLine: function() {
 this.$http.post('/lists.json', { item: this.checkInput }, {}).then(response => {
  this.fetchLists(), this.checkInput = ''
 }).catch(function(error) {
  console.log('Got a problem' + error)
 })
}

catch用于全程出错后,对错误的处理。

deleteLine(id) {
 this.$http.delete(`/lists/${id}.json`).then(response => {
 this.fetchLists()
})


disabled属性:

当true时,元素不能用。包括<button><input><select><textarea><options>

<input type='text' v-model='line' class='form-control' autofocus="true">
<button v-on:click='addLine()' class='btn btn-primary' :disabled="!line.length" >Add line</button>

line是string。调用!line.length方法, 结果有2种:

  • !0    >  true     line是空字符串。
  • !12  > false  line不为空。

JS的Array的map方法

例子:

var persons = [
  {firstname : "Malcom", lastname: "Reynolds"},
  {firstname : "Kaylee", lastname: "Frye"},
  {firstname : "Jayne", lastname: "Cobb"}
];

persons.map((old) => { old.secondname = 'hello'; return old})

结果

[

{firstname: "Malcom", lastname: "Reynolds", secondname: "hello"},

{firstname: "Kaylee", lastname: "Frye", secondname: "hello"},

{firstname: "Jayne", lastname: "Cobb", secondname: "hello"}

]

arr.map(function(old) { return new })


用Rails.5.2+ Vue.js做 vue-todolist app的更多相关文章

  1. Vue.js起手式+Vue小作品实战

    本文是小羊根据Vue.js文档进行解读的第一篇文章,主要内容涵盖Vue.js的基础部分的知识的,文章顺序基本按照官方文档的顺序,每个知识点现附上代码,然后根据代码给予个人的一些理解,最后还放上在线编辑 ...

  2. js 引入Vue.js实现vue效果

    拆分组件为单个js见:https://www.jianshu.com/p/2f0335818ceb 效果 html <!DOCTYPE html> <html> <hea ...

  3. 学习Vue.js之vue移动端框架到底哪家强

    官网:https://cn.vuejs.org/. 转载:http://www.cnblogs.com/8899man/p/6514212.html Weex 2016年4月21日,阿里巴巴在Qcon ...

  4. 花了几天学习了vue跟做的仿制app

    Vue.js国内开发者 是用于构建交互式的 Web  界面的库.它提供了mvvm 数据绑定和一个可组合的组件系统,具有简单.灵活的 API.从技术上讲, Vue.js 集中在 mvvm 模式上的视图模 ...

  5. Vue.js之Vue计算属性、侦听器、样式绑定

    前言 上一篇介绍了Vue的基本概念,这一篇介绍一下Vue的基本使用. 一.搭建一个Vue程序 1.1 搭建Vue环境 搭建Vue的开发环境总共有三种方法: 引入CDN <script src=& ...

  6. Vue.js 入门 --- vue.js 安装

    本博文转载  https://blog.csdn.net/m0_37479246/article/details/78836686 Vue.js(读音 /vjuː/, 类似于 view)是一个构建数据 ...

  7. vue.js实战——vue元素复用

    Vue在渲染元素时,出于效率考虑,会尽可能地复用已有的元素而非重新渲染,例: <!DOCTYPE html> <html lang="en"> <he ...

  8. vue.js实战——vue 实时时间

    created:实例创建完成后调用,此阶段完成了数据的观测等,但尚未挂载,$el还不可用,需要初始化处理一些数据时会比较有用. mounted:el挂载到实例上后调用,一般我们的第一个业务逻辑会在这里 ...

  9. vue.js利用vue.router创建前端路由

    node.js方式: 利用node.js安装vue-router模块 cnpm install vue-router 安装完成后我们引入这个模板! 下载vue-router利用script引入方式: ...

随机推荐

  1. 20154312 曾林 EXP6 信息搜集与漏洞扫描

    目录 1.实验后回答问题 2.实验总结与体会 3.实践过程记录 --3.1.信息收集 ----3.1.1.whois查询 ----3.1.2.nslookup,dig查询 ----3.1.3.trac ...

  2. FastDFS+nginx+keepalived集群搭建

    安装环境 nginx-1.6.2 libfastcommon-master.zip FastDFS_v5.05.tar.gz(http://sourceforge.net/projects/fastd ...

  3. linux常用命令:free 命令

    free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存,及被内核使用的buffer.在Linux系统监控的工具中,free命令是最经常使用的命令之一. 1.命令格式: free [参 ...

  4. pythonl类继承例子

    #coding=utf-8 class Person(object):    def __init__(self,name,age):        self.name=name        sel ...

  5. 修改MySQL数据库中表和表中字段的编码方式的方法

    今天向MySQL数据库中的一张表添加含有中文的数据,可是老是出异常,检查程序并没有发现错误,无奈呀,后来重新检查这张表发现表的编码方式为latin1并且原想可以插入中文的字段的编码方式也是latin1 ...

  6. nginx 参考文章汇总

    Nginx 反向代理.负载均衡.页面缓存.URL重写及读写分离详解: http://freeloda.blog.51cto.com/2033581/1288553 Nginx开发从入门到精通: htt ...

  7. Google's Machine Learning Crash Course #03# Reducing Loss

    Goal of training a model is to find a set of weights and biases that have low loss, on average, acro ...

  8. 深入理解Java虚拟机 #01# 自己编译JDK

    x 首先用书上的脚本尝试,失败. 之后根据源文件的 README 编译,抛出: root@linux:/opt/openjdk# sh ./get_source.sh ERROR: Need init ...

  9. Linux 安装JDK(jdk-8u121-linux-x64.tar.gz)

    Linux 安装JDK(jdk-8u121-linux-x64.tar.gz) 一.JDK准备 1.1 文件名称 jdk-8u121-linux-x64.tar.gz 1.2 下载地址 http:// ...

  10. Linux内存管理--用户空间和内核空间【转】

    本文转载自:http://blog.csdn.net/yusiguyuan/article/details/12045255 关于虚拟内存有三点需要注意: 4G的进程地址空间被人为的分为两个部分--用 ...