首页 > 移动平台 > 详细

用Rails.5.2+ Vue.js做 vue-todolist app

时间:2018-09-14 13:06:41      阅读:242      评论:0      收藏:0      [点我收藏+]

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


注意: Edit/update使用SPA(不换页)的方法完成。点击文字出现一个输入框和按钮。

我的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

原文:https://www.cnblogs.com/chentianwei/p/9645913.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!