v_Cloud 2019-02-27 10:47 采纳率: 50%
浏览 1787

vue 构建tabs组件,插槽slots内容更新问题

在构建tab组件的时候,涉及内容更新遇到问题
在更新内容的时候,总会延迟一步更新插槽内容
在跟某课网学习过程中,他的方案是把插槽内容封装成组件,插槽数据当成props传入,以此来解决这个问题

但在我实际的操作过程中,把内容封装成组件后,插槽不会更新了
图片说明

<script type="text/jsx">
  import TabContainer from './tab-container.vue'
  export default {
    name: 'Tabs',
    components:{
      TabContainer
    },
    provide () {
      const data = {}
      Object.defineProperty(data, 'value', { // 时时get数据,get方法
        get: () => this.value,
        enumerable: true
      })
      return {
        value : this.value,
        data
      }
    },
    data () {
      return {
        panes: []
      }
    },
    props: {
      value: {
        type: [String, Number],
        required: true
      }
    },
    render () {
      const contents = this.panes.map(pane => {
        return pane.active ? pane.$slots.default : null
      })
      return (
        <div class='tabs'>
          <ul class='tabs-header'>
            {this.$slots.default}
          </ul>
          <div className="tab-content">
            {contents}
          </div>
          <tab-container panes={this.panes}></tab-container>
        </div>
      )
    },
    methods:{
      onChange (index) {
        this.$emit('change', index)
      }
    }
  }
</script>

拆分的组件代码

<script type="text/jsx">
  export default {
    props: {
      panes: {
        type: Array,
        required: true
      }
    },
    render () {
      const contents = this.panes.map(pane => {
        return pane.active ? pane.$slots.default : null
      })
      return (
        <div class="tab-content">
          {contents}
        </div>
      )
    }
  }
</script>
  • 写回答

1条回答

  • yml19960625 2021-01-11 17:01
    关注

    我也遇到了这个问题,这个您解决了吗?

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序