父组件改变子组件的值

子组件:com-tab.vue 

<template>
  <div class="flex-between">
    <div
      :style="{ width: item.width + 1 + 'rem', height: fatherBoxHeight + 'rem' }"
      v-for="(item, index) in tabCont"
      :key="index"
      @click="changeTab(index,item)"
    >
      <div
        class="com-tab"
        :style="{ width: item.width + 1 + 'rem', height: fatherBoxHeight + 'rem' }"
      >
        <div class="blue-box">
          <div
            class="blue-cont"
            :style="{ width: item.width + 'rem', height: boxHeight + 'rem' }"
          ></div>
        </div>
        <div class="white-box" :class="{ active: tabIdx == index }">
          <div
            class="white-cont"
            :style="{ width: item.width + 'rem', height: boxHeight + 'rem' }"
            :class="{ activeCont: tabIdx == index }"
          >
            <div
              class="tab-cont-item flex-center"
              
            >
              {{ item.text }}
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
  
  <script>
export default {
  components: {},
  props: {
    boxHeight: {
      type: Number,
      default: 3.95,//不传默认3.95rem
    },
    tabIdx: {
      type: Number,
      default:0,//不传默认3.95rem
    },
    colorActive: {
      type: Boolean,
      default: false,
    },
    tabCont: {
      type: Array,
      default: [],
    },
  },
  data() {
    return {
      fatherBoxHeight: 0, // 屏幕高度
    };
  },
  computed: {},
  watch: {},
  methods: {
    changeTab(index,item) {
      this.tabIdx = index;
      // 将选中的下标传给父组件
      this.$emit('changeValue',this.tabIdx,item);
    },
  },
  created() {
   
  },
  mounted() {
    this.fatherBoxHeight = this.boxHeight + 1.5;
  },
  beforeCreate() {},
  beforeMount() {},
  beforeUpdate() {},
  updated() {},
  beforeDestroy() {},
  destroyed() {},
  activated() {},
};
</script>
  <style lang='scss' scoped>
.flex-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}
.com-tab {
  position: relative;
  margin: 0 auto;

  //
  .blue-box {
    position: absolute;
    left: 0.5rem;
    top: 0.5rem;
    z-index: 66;
    padding: 2px; //边框的宽度
    background: linear-gradient(-45deg, transparent 9px, rgba(176, 192, 255, 1) 0);
  }
  .blue-cont {
    background: linear-gradient(-45deg, transparent 8px, #fff 0);
  }
  .white-box {
    position: absolute;
    left: 0;
    top: 0;
    z-index: 77;
    padding: 2px; //边框的宽度
    background: linear-gradient(-45deg, transparent 9px, rgba(176, 192, 255, 1) 0);
    font-size: 1.3rem;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #000000;
    letter-spacing: 0.2rem;
  }
  .white-cont {
    background: linear-gradient(-45deg, transparent 8px, #fff 0);
  }
  .active {
    position: absolute;
    left: 0;
    top: 0;
    z-index: 77;
    padding: 2px; //边框的宽度
    background: linear-gradient(-45deg, transparent 9px, rgba(0, 52, 255, 1) 0);
  }
  .activeCont {
    background: linear-gradient(-45deg, transparent 8px, rgba(0, 52, 255, 1) 0);
    color: #ffffff;
  }
  .horizontal-purple-square-position {
    position: absolute;
    right: 0;
    top: 0;
    z-index: 9999;
  }
  .tab-cont-item {
    width: 100%;
    height: 100%;
  }

}
</style>

父组件改变了子组件的tabIdx的值:

正确的子组件:com-tab.vue 

改动:使用currentIndex接收父组件传过来的tabIdx的值,并在组件内原本直接使用tabIdx的地方替换成currentIndex

<template>
  <div class="flex-between">
    <div
      :style="{ width: item.width + 1 + 'rem', height: fatherBoxHeight + 'rem' }"
      v-for="(item, index) in tabCont"
      :key="index"
      @click="changeTab(index,item)"
    >
      <div
        class="com-tab"
        :style="{ width: item.width + 1 + 'rem', height: fatherBoxHeight + 'rem' }"
      >
        <div class="blue-box">
          <div
            class="blue-cont"
            :style="{ width: item.width + 'rem', height: boxHeight + 'rem' }"
          ></div>
        </div>
        <div class="white-box" :class="{ active: currentIndex == index }">
          <div
            class="white-cont"
            :style="{ width: item.width + 'rem', height: boxHeight + 'rem' }"
            :class="{ activeCont: currentIndex == index }"
          >
            <div
              class="tab-cont-item flex-center"
              
            >
              {{ item.text }}
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
  
  <script>
export default {
  components: {},
  props: {
    boxHeight: {
      type: Number,
      default: 3.95,//不传默认3.95rem
    },
    tabIdx: {
      type: Number,
      default:0,//不传默认3.95rem
    },
    colorActive: {
      type: Boolean,
      default: false,
    },
    tabCont: {
      type: Array,
      default: [],
    },
  },
  data() {
    return {
      fatherBoxHeight: 0, // 屏幕高度
      currentIndex:this.tabIdx//currentIndex接收父组件传来的tabIdx值
    };
  },
  computed: {},
  watch: {},
  methods: {
    changeTab(index,item) {
      this.currentIndex = index;
      // 将选中的下标传给父组件
      this.$emit('changeValue',this.currentIndex,item);
    },
  },
  created() {
   
  },
  mounted() {
    this.fatherBoxHeight = this.boxHeight + 1.5;
  },
  beforeCreate() {},
  beforeMount() {},
  beforeUpdate() {},
  updated() {},
  beforeDestroy() {},
  destroyed() {},
  activated() {},
};
</script>
  <style lang='scss' scoped>
.flex-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}
.com-tab {
  position: relative;
  margin: 0 auto;
  //
  .blue-box {
    position: absolute;
    left: 0.5rem;
    top: 0.5rem;
    z-index: 66;
    padding: 2px; //边框的宽度
    background: linear-gradient(-45deg, transparent 9px, rgba(176, 192, 255, 1) 0);
  }
  .blue-cont {
    background: linear-gradient(-45deg, transparent 8px, #fff 0);
  }
  .white-box {
    position: absolute;
    left: 0;
    top: 0;
    z-index: 77;
    padding: 2px; //边框的宽度
    background: linear-gradient(-45deg, transparent 9px, rgba(176, 192, 255, 1) 0);
    font-size: 1.3rem;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #000000;
    letter-spacing: 0.2rem;
  }
  .white-cont {
    background: linear-gradient(-45deg, transparent 8px, #fff 0);
  }
  .active {
    position: absolute;
    left: 0;
    top: 0;
    z-index: 77;
    padding: 2px; //边框的宽度
    background: linear-gradient(-45deg, transparent 9px, rgba(0, 52, 255, 1) 0);
  }
  .activeCont {
    background: linear-gradient(-45deg, transparent 8px, rgba(0, 52, 255, 1) 0);
    color: #ffffff;
  }
  .horizontal-purple-square-position {
    position: absolute;
    right: 0;
    top: 0;
    z-index: 9999;
  }
  .tab-cont-item {
    width: 100%;
    height: 100%;
  }

}
</style>

 immersive.vue使用com-tab.vue

<template>
<div>

        <com-tab :tab-idx="tabIdx" :tab-cont="tabCont" @changeValue="changeValue" ></com-tab>
 <div class="live-panel" v-show="tabIdx == 0">
           tab0内容
          </div>
 <div class="live-panel" v-show="tabIdx == 1">
           tab1内容
          </div>
 <div class="live-panel" v-show="tabIdx == 2">
           tab2内容
          </div>
</div>
</template>
<script>


export default {
  name: 'immersive',
  components: {

    'com-tab': () => import('@/pages/fbec-game/components/com-tab.vue'),//子组件
   
  },
  data() {
 tabCont: [
        { width: 10.1, text: '现场直播' },
        { width: 10.1, text: '视频列表' },
        { width: 10.1, text: '云相册直击' },
      ],
      tabIdx: 1, //选中的tab下标,传给子组件的值
};
  },
  methods: {
    // 获取下标
    changeValue(data) {
      this.tabIdx = data;
    },
}
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue中,组件可以通过props向组件传递数据,但是组件不能直接修改props中的。如果需要修改props中的,可以通过在组件中定义一个与props同名的data属性,并在组件中使用计算属性或者watch监听props的变化,从而实现组件改变组件。 以下是一个简单的示例代码: 组件: ```vue <template> <div> <button @click="changeChildValue">改变组件</button> <ChildComponent :child-value="parentValue"></ChildComponent> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { components: { ChildComponent }, data() { return { parentValue: '组件' } }, methods: { changeChildValue() { this.parentValue = '改变后的组件' } } } </script> ``` 组件: ```vue <template> <div> <p>组件:{{ childValue }}</p> </div> </template> <script> export default { props: { childValue: { type: String, default: '' } }, data() { return { childData: '' } }, computed: { // 监听props的变化,将props的赋给组件的data属性 childValueComputed: { get() { return this.childValue }, set(val) { this.$emit('update:childValue', val) } } }, watch: { // 监听props的变化,将props的赋给组件的data属性 childValue(val) { this.childData = val } } } </script> ``` 在组件中,我们定义了一个与props同名的data属性childValue,并使用computed监听props的变化,将props的赋给组件的data属性。同时,我们还使用了$emit方法向组件发送一个update事件,从而实现组件改变组件。 在组件中,我们通过v-bind指令将组件parentValue传递给组件的props属性childValue,并在点击按钮时改变组件,从而触发组件的更新。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值