账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vue操作子组件dom样式问题
    • 2019-08-07 00:00
    • 11
    41
    0

    子组件:
    在computed里面设置的style

     <div class="main fixed bg_fff t0 b0 flex column oh" id="video_box" :style="style"  v-if="showTag"></div>
    computed: {
          style() {
            const width = this.width;
            return {
              top: '40px',
              left: `${this.W / 2}px`,
              marginLeft: `${0 - width / 2}px`,
              width: `${width}px`
            };
          }
        }

    这个子组件是能够拖动的fixed,现在的需求:在他拖动这个子组件以后,父组件或者子组件获取到这个子组件的top值(距离页面顶部的距离),然后重新给他赋值。
    父组件:
    引用子组件

    <consultation-video-dialog ref="videoDialog" :id="consultId" :room-id="room.roomNo" class="video_box"></consultation-video-dialog>
        </div>
    console.log(this.$refs.videoDialog.style.cssText); // 父组件打印 返回undefined

    尝试改变this.$refs.videoDialog.style.top,但是拖动以后子组件里的style的top一直是初始化的40px,获取到没有什么意义
    怎么才能给子组件样式动态赋值呢

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 尽⊙欢 普通会员 1楼

      在Vue中,可以通过两种方式来操作子组件的DOM样式:

      1. 使用$refs属性 <template>标签内:

      html <child-component ref="childRef"></child-component>

      <script>标签内:

      javascript this.$refs.childRef.style.css("color", "red");

      1. 使用计算属性 <template>标签内:

      html <child-component v-style="{color: 'red'}"></child-component>

      <script>标签内:

      javascript this.$refs.childRef.style = { color: 'red' };

      以上两种方式都可以实现对子组件DOM样式的操作,具体使用哪种方式,取决于你实际的需求和使用的场景。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部