vue的常用组件操作方法应用分析
作者:adouwt 时间:2024-05-21 10:17:13
项目技术:
webpack + vue + element + axois (vue-resource) + less-loader+ ...
vue的操作的方法案例:
1.数组数据还未获取到,做出预加载的动画
<el-carousel :interval="3000" type="card" height="200px" class="common-mt-md">
<el-carousel-item v-for="item in movieArr" :key="item.id" class="text-center">
<img v-bind:src="item.images.small" alt="电影封面" class="ticket-index-movie-img">
</el-carousel-item>// 实际显示的内容-跑马灯
<div v-if="!movieArr.length" class="ticket-index-movie-loading">
<span class="el-icon-loading "></span>
</div>// 当 movirArr的数组为空的时候,做出的预加载 loading
</el-carousel>
2. 按钮状态的判断,按钮能不能点的问题
<p v-if="!multipleSelection.length">
<el-button type="success" round disabled>导出</el-button>
</p><!-- 不能点, 判断数组为空 -->
<p v-else>
<el-button type="success" round >导出</el-button>
</p><!-- 可以点, 判断数组为不为空 -->
3.像jquery 一样,追加dom (vue 是以数据为导向的,应该摆脱jquery的 dom的繁杂操作)
<el-form-item label="时间" prop="name">
<el-input v-model="ruleForm.name"></el-input>//绑定模型,检测输入的格式
<span class="el-icon-plus ticket-manage-timeinput" @click="addTime(this)"></span>//绑定方法,增加dom的操作
</el-form-item>
<el-form-item label="时间" prop="name" v-for="item in timeArr" :key='item.id'> //timeArr数组与数据就渲染下面的dom,没有就不显示
<el-input v-model="ruleForm.name"></el-input>
<span class="el-icon-minus ticket-manage-timeinput" @click="minusTime(this)"></span>
</el-form-item>
js:
相当于jq 中的 dom 字符串
timeInputString: '<el-input v-model="ruleForm.name"></el-input><span class="el-icon-minus"></span>'
原生的js 往数组里压入和弹出 数据(抓数组的长度),因为vue的是以数据驱动,以数据判断,该不该渲染dom
addTime () {
this.timeArr.push('str')
},
minusTime () {
this.timeArr.shift('str')
}
4. 追加class , 场景 在循环某个列表时候,某个列表有class,绑定一个方法,可以支持穿参数
dom
<li v-for="section in item.sections" :key='section.id' @click="hideParMask" :class="getSectionId(section.id)">
<router-link :to="{ name: 'learning', params: { sectionId: section.id}, query: { courseId: courseId}}" >
<span>{{item.orderInCourse}}.{{section.sectionNumber}}</span>
<span>{{section.name}}</span>
</router-link>
</li>
js
getSectionId (sectionId) {
return {
active: this.$route.params.sectionId === sectionId,
}
}
5.子->父组件的通信,vue.$emit vue.on
子组件:
getSectionId (sectionId) {
return {
active: this.$route.params.sectionId === sectionId,
}
}
父组件:
dom
<v-child :courseId="courseId" v-on:receiveTitle="receiveTitle"></v-child>
js
methods: {
receiveTitle (name) {
this.titleName = name; // titleName 就是 **@课程
}
}总结套路:子组件使用函数(事件)给父组件传递 receiveTitle 属性,然后父组件监测这个属性,给这个属性绑定方法 receiveTitle,方法传参数,这个参数就是 要传递的 值
6.父-> 子
父组件:
dom:
<course-tab :courseList = courseList ></course-tab>
js:
courseList().then(res => {
this.courseList = res.data.courses;
}).catch( err => {
console.log(err)
});
子组件:
props: {
courseList: {
type: Array
}
}
总结套路:父组件将变量传到子组件,需要在子组件标签上绑定这个变量,然后子组件就可以在props 里接受这个变量
7.错误路由的处理,重定向, 在router里添加一个路由信息
{
path: '*',
redirect: '/'
}
这里是重新定向到首页,也可以单独做一个 404页面,重定向到这个页面
编程式导航里面,
router.push({ path: 'login-regist' }) // 如果这样写的话,会寻找路由最近的 / 然后在后面直接拼接login-regist;
为了防止在多级嵌套路由里面出现bug ,应该写全路由的全部信息,包括 /
router.push({ path: '/login-regist' })
8. dom 里拼接css
<div class="img" :style="{background: 'url(' + item.logoFileURL + ')'}"></div>
9. 监听滚动事件
data () {
return {
scrolled: false,
show: true
}
},
methods: {
handleScroll () {
this.scrolled = window.scrollY > 0;
if (this.scrolled) {
this.show = false;
}
}
},
mounted () {
window.addEventListener('scroll', this.handleScroll);
}
10.监听输入框输入值的变化
@input="search",
监听 element-UI 的<el-input
的方法,
<el-input v-model="input" @keyup.enter.native="add" placeholder="请输入内容" ></el-input>
总结
以上所述是小编给大家介绍的vue的常用组件操作方法应用分析网站的支持!
来源:http://www.cnblogs.com/adouwt/p/7911639.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
TypeScript类型检查详谈及火爆原因
![](https://img.aspxhome.com/file/2023/1/131811_0s.png)
Python一行代码对话ChatGPT实现详解
![](https://img.aspxhome.com/file/2023/7/85367_0s.jpg)
Vue实现web分页组件详解
![](https://img.aspxhome.com/file/2023/2/130102_0s.gif)
Python中使用glob和rmtree删除目录子目录及所有文件的例子
使用python Telnet远程登录执行程序的方法
JavaScript中定义函数的三种方法
python冒泡排序简单实现方法
python3安装OCR识别库tesserocr过程图解
![](https://img.aspxhome.com/file/2023/9/128519_0s.png)
好用的Python编辑器WingIDE的使用经验总结
![](https://img.aspxhome.com/file/2023/0/100290_0s.png)
Python高级特性与几种函数的讲解
Python3基于sax解析xml操作示例
![](https://img.aspxhome.com/file/2023/6/123506_0s.png)
在oracle 数据库查询的select 查询字段中关联其他表的方法
MySQL条件查询语句常用操作全面汇总
![](https://img.aspxhome.com/file/2023/4/92754_0s.png)
利用Python绘制虎年烟花秀
![](https://img.aspxhome.com/file/2023/0/121770_0s.png)
python实现会员信息管理系统(List)
![](https://img.aspxhome.com/file/2023/5/88965_0s.jpg)
python列表[list]和元组(tuple)详情
mysql中格式化数字详解
设计和企业文化
kafka rabbitMQ及rocketMQ队列的消息可靠性保证分析
![](https://img.aspxhome.com/file/2023/2/130892_0s.png)