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

标签:vue,常用组件
0
投稿

猜你喜欢

  • TypeScript类型检查详谈及火爆原因

    2022-01-24 11:48:41
  • Python一行代码对话ChatGPT实现详解

    2022-04-24 04:07:26
  • Vue实现web分页组件详解

    2024-04-30 10:39:27
  • Python中使用glob和rmtree删除目录子目录及所有文件的例子

    2023-12-26 07:14:18
  • 使用python Telnet远程登录执行程序的方法

    2022-12-22 11:56:57
  • JavaScript中定义函数的三种方法

    2024-05-09 10:37:04
  • python冒泡排序简单实现方法

    2022-09-27 12:28:57
  • python3安装OCR识别库tesserocr过程图解

    2021-12-08 04:50:08
  • 好用的Python编辑器WingIDE的使用经验总结

    2022-01-15 06:23:10
  • Python高级特性与几种函数的讲解

    2021-12-09 03:37:17
  • Python3基于sax解析xml操作示例

    2022-06-07 10:18:52
  • 在oracle 数据库查询的select 查询字段中关联其他表的方法

    2009-08-31 12:27:00
  • MySQL条件查询语句常用操作全面汇总

    2024-01-26 11:10:24
  • 利用Python绘制虎年烟花秀

    2022-10-08 06:03:49
  • python实现会员信息管理系统(List)

    2021-12-21 04:50:45
  • python列表[list]和元组(tuple)详情

    2022-05-23 01:14:31
  • mysql中格式化数字详解

    2024-01-23 16:44:07
  • 设计和企业文化

    2009-03-28 10:35:00
  • kafka rabbitMQ及rocketMQ队列的消息可靠性保证分析

    2022-05-06 08:10:23
  • 淘宝CSS框架研究(1):Reset CSS(八卦篇)

    2009-03-31 12:58:00
  • asp之家 网络编程 m.aspxhome.com