2019.12.24,天气晴,居然都忘了今晚平安夜。。。。
目录
谈一谈工作中遇到的问题记录:
学习点参考下面文章:
思考点:
@touchstart====>点击就会触发,不会延迟
@click===>在ios上会有300ms延迟
但是做移动端页面都是禁止缩放的,这种情况fastclick也没必要使用,直接@click就行了
注意点:touchstart有个缺陷,比如按钮在屏幕中是@touchstart事件,但是为只是想点击滑动看下面的内容
(动作为:按屏幕==>往上拖动===>手指离开屏幕),按屏幕的动作不小心点到按钮再往上拖动,“按屏幕”瞬间就会触发@touchstart对应方法的逻辑。
@touchend在点击滑动的时候,点击到按钮再往上拖动,当手指离开屏幕的瞬间触发@touchend对应方法的逻辑。
记录自己看视频学习的点:
也就是一个搜索功能,先上图,后上视频代码
原始图:
当我添加一项Id:123 Name:hehehe之后
点击删除id为2的宝马之后,先alert索引号(从0开始)
确定之后就被删除
视频代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./lib/vue-2.4.0.js"></script>
<link rel="stylesheet" href="./lib/bootstrap-3.3.7.css">
</head>
<body>
<div id="app">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">添加品牌</h3>
</div>
<div class="panel-body form-inline">
<label>
Id:
<input type="text" class="form-control" v-model="id">
</label>
<label>
Name:
<input type="text" class="form-control" v-model="name">
</label>
<!-- 在Vue中,使用事件绑定机制,为元素指定处理函数的时候,如果加了小括号,就可以给函数传参了 -->
<input type="button" value="添加" class="btn btn-primary" @click="add()">
<label>
搜索名称关键字:
<input type="text" class="form-control" v-model="keywords">
</label>
</div>
</div>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Ctime</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<!-- 之前, v-for 中的数据,都是直接从 data 上的list中直接渲染过来的 -->
<!-- 现在, 我们自定义了一个 search 方法,同时,把 所有的关键字,通过传参的形式,传递给了 search 方法 -->
<!-- 在 search 方法内部,通过 执行 for 循环, 把所有符合 搜索关键字的数据,保存到 一个新数组中,返回 -->
<tr v-for="item in search(keywords)" :key="item.id">
<td>{{ item.id }}</td>
<td v-text="item.name"></td>
<td>{{ item.ctime }}</td>
<td>
<a href="" @click.prevent="del(item.id)">删除</a>
</td>
</tr>
</tbody>
</table>
</div>
<script>
// 创建 Vue 实例,得到 ViewModel
var vm = new Vue({
el: '#app',
data: {
id: '',
name: '',
keywords: '', // 搜索的关键字
list: [
{ id: 1, name: '奔驰', ctime: new Date() },
{ id: 2, name: '宝马', ctime: new Date() },
{ id: 3, name: 'TPshop', ctime: new Date() }
]
},
methods: {
add() { // 添加的方法
// console.log('ok')
// 分析:
// 1. 获取到 id 和 name ,直接从 data 上面获取
// 2. 组织出一个对象
// 3. 把这个对象,调用 数组的 相关方法,添加到 当前 data 上的 list 中
// 4. 注意:在Vue中,已经实现了数据的双向绑定,每当我们修改了 data 中的数据,Vue会默认监听到数据的改动,自动把最新的数据,应用到页面上;
// 5. 当我们意识到上面的第四步的时候,就证明大家已经入门Vue了,我们更多的是在进行 VM中 Model 数据的操作,同时,在操作Model数据的时候,指定的业务逻辑操作;
var car = { id: this.id, name: this.name, ctime: new Date() }
this.list.push(car)
this.id = this.name = ''
},
del(id) { // 根据Id删除数据
// 分析:
// 1. 如何根据Id,找到要删除这一项的索引
// 2. 如果找到索引了,直接调用 数组的 splice 方法
/* this.list.some((item, i) => {
if (item.id == id) {
this.list.splice(i, 1)
// 在 数组的 some 方法中,如果 return true,就会立即终止这个数组的后续循环
return true;
}
}) */
var index = this.list.findIndex(item => item.id == id)
alert(index);
// console.log(index)
this.list.splice(index, 1)
},
search(keywords) { // 根据关键字,进行数据的搜索
/* */
var newList = []
this.list.forEach(item => {
if (item.name.indexOf(keywords) != -1) {
newList.push(item)
}
})
return newList
// 注意: forEach some filter findIndex 这些都属于数组的新方法,
// 都会对数组中的每一项,进行遍历,执行相关的操作;
/*
// 第二种方法
var arr_list = this.list.filter(item => {
if (item.name.includes(keywords)) {
return item
}
})
return arr_list
*/
/*
// 第三种方法
var arr_list = this.list.filter(item => item.name.includes(keywords))
return arr_list;
*/
/*
// 第四种方法
var arr_list = this.list.filter(item => true)
return arr_list;
*/
// return newList
}
}
});
</script>
</body>
</html>
在这个代码中看到了<td v-text="item.name"></td>,这里用了v-text,而v-text有什么用呢?
上篇周记说到插值表达式有一个问题:
页面频繁刷新或者网速加载很慢的时候,页面会先出现“{{ msg }}”,再一闪而过出现真实的数据,可以用v-cloak解决
v-text也是一样的功能!!那岂不是指令重复了??一个功能干嘛2个指令呢?
插值表达式可以把Vue传递过来的数据和前台用户自己定义的数据结合在一起,但是v-text只能显示Vue对象传递过来的数据,会替换掉节点里已有的内容。
更详细的解释见这里:Vue学习之路第三篇:插值表达式和v-text的区别
===============Talk is cheap, show me the code================