跟随Element学习Vue小技巧(42)——Steps

932 阅读2分钟

笔直前进

说到做到

前言

一步一个台阶,成功其实很简单嘛 ^_^
Take it eazy,step by step

1 Steps

盒子尺寸 flex-basis

A B C D E ?

代码片段

技巧解析

弹性盒子,顾名思义具有弹性
怎么才能让盒子固定呢?
width,height,你会发现没有作用
除非,除非,flex:none
除此之外呢?
还有flex-basis属性

flex-basis 传送门

2 Step

父链子链

儿子可以找父亲要零花钱
父亲当然可以找儿子要赡养费

beforeCreate() {
  this.$parent.steps.push(this);
},

beforeDestroy() {
  const steps = this.$parent.steps;
  const index = steps.indexOf(this);
  if (index >= 0) {
    steps.splice(index, 1);
  }
},
updateStatus(val) {
  const prevChild = this.$parent.$children[this.index - 1];

  if (val > this.index) {
    this.internalStatus = this.$parent.finishStatus;
  } else if (val === this.index && this.prevStatus !== 'error') {
    this.internalStatus = this.$parent.processStatus;
  } else {
    this.internalStatus = 'wait';
  }

  if (prevChild) prevChild.calcProgress(this.internalStatus);
},

技巧解析

作为父亲,this.$children找儿子
作为儿子,this.$parent找父亲
兄弟咋整? 找父亲的儿子咯
this.$parent.$children

动态watch

好看不?
要不,今天换个望远镜看看

mounted() {
  const unwatch = this.$watch('index'val => {
    this.$watch('$parent.active'this.updateStatus, { immediate: true });
    this.$watch('$parent.processStatus', () => {
      const activeIndex = this.$parent.active;
      this.updateStatus(activeIndex);
    }, { immediate: true });
    unwatch();
  });
}

技巧解析

动态添加watch,this.$watch
三个参数: data + handler + option
怎么移除呢? unwatch,不观察就好

Vue中watch使用 传送门



我动身踏上旅程 是为了与你道别

参考链接

往期回顾