el-dialog每次重新打开展示不同的内容,滚动条未重新置顶的解决方案。

环境

nuxt3 + vu3 + element-plus-2.3.3

复现原因

假设有5条内容,打开一项内容通过el-dialog进行展示,当出现滚动条后,往下滑动,紧接着通过esc或遮罩进行关闭,打开另一项内容,这时候滚动条并未进行置顶。

解决方法

参考就行了。

js部分

const data = reactive({
  newsDialog: {
    show: false,
    currentNews: null,
  }
})

const refNewsDialog = ref(null);
watchEffect( ()=> {
  if(data.newsDialog.currentNews !== null){
    console.log("打开对话框", data.newsDialog.currentNews)
    data.newsDialog.show = true;
    nextTick(() => {
      // 滚动条重置
      ==refNewsDialog.value.dialogContentRef.$el.parentElement.scroll(0,0);==
    })
  }
})

模板部分

    <client-only>
      <el-dialog
          destroy-on-close ref="refNewsDialog" v-model="data.newsDialog.show" :show-close="true" @closed="data.newsDialog.currentNews = null" width="80%" class="news-dialog">
        <template #header="{ close, titleId, titleClass }">
          <div class="header">
            <h4 :id="titleId" :class="titleClass" v-text="data.newsDialog.currentNews.title"></h4>
          </div>
        </template>
        <div class="news-content" v-html="data.newsDialog.currentNews.content"></div>
      </el-dialog>
    </client-only>

总结

vue3因为对组件有更精细的控制暴露出的对象信息,这导致了element-plus2版本暴露出的有用对象太少了。真是太难了。

posted @   星小梦  阅读(1362)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示