欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

在实战案例中vue组件使用

程序员文章站 2022-04-18 23:29:32
...
这次给大家带来在实战案例中vue组件使用,在实战案例vue组件使用的注意事项有哪些,下面就是实战案例,一起来看一下。

z项目技术:

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>// 实际显示的内容-跑马灯
   <p v-if="!movieArr.length" class="ticket-index-movie-loading">
    <span class="el-icon-loading "></span>
   </p>// 当 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

<p class="img" :style="{background: 'url(' + item.logoFileURL + ')'}"></p>

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>

相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!

推荐阅读:

React内render案例详解

Vue Mixin功能使用案例详解

以上就是在实战案例中vue组件使用的详细内容,更多请关注其它相关文章!

相关标签: vue 常用组件