亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據另一個元素 vue 的屬性有條件地渲染 div

根據另一個元素 vue 的屬性有條件地渲染 div

jeck貓 2022-07-08 17:41:55
我有一個小文本容器。我想要做的是如果文本長度很大,折疊 div,然后有一個顯示“...顯示更多”的按鈕,一旦按下就會展開 div。再次按下折疊 div。這很好并且有效。我現在有一個問題。div 最初設置為 collapse=true。顯示“...顯示更多”按鈕。我要更改的是,如果文本內容不長,則不會折疊,不會顯示顯示更多按鈕。模板  <v-card v-show="showAccount" class="mb-4">          <v-card-title class="title-container align-start">      <div class="title-data" :class="{collapsed: isElementOverflown}" ref="title-data">        <h1 class="title mb-2"><router-link :to="{name: 'profile', params: {account: account.account}}">{{ account.account }}</router-link></h1>        <router-link v-if="isActiveUserAccount" :to="{name: 'account-image', params: {account: account.account}}">          <v-avatar color="#c35219" size="56" class="mr-4 mb-2">            <img v-if="accountMedia" :src="accountMedia" :alt="account.account" />            <span v-else class="white--text headline">{{ account.account[0].toUpperCase() }}</span>          </v-avatar>        </router-link>        <template v-else>          <v-avatar color="#c35219" size="56" class="mr-4 mb-2">            <img v-if="accountMedia" :src="accountMedia" :alt="account.account" />            <span v-else class="white--text headline">{{ account.account[0].toUpperCase() }}</span>          </v-avatar>        </template>        <div class="caption my-0" ref="bio">          <nl2br v-if="account.about" tag="p" :text="account.about"></nl2br>        </div>      </div>      <button v-if="showButton" type="button" style="font-size:small; margin: auto; margin-right: 5%" @click="toggleHeight">        {{showMoreTextLabel}}        </button>    </v-card-title>在mounted我收到一個 this.$refs[“title-data”]未定義但 This.$refs存在的錯誤,它顯示了正確的參考。我不確定為什么。感謝您的任何幫助!
查看完整描述

2 回答

?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

您可以創建一個計算屬性來檢查文本的長度是否超過給定的數字。


    computed: {

             isTextLengthLongEnough() {

              if(el.offsetHeight > 150) {

              this.showButton = true;

                }

              }

然后,您可以使用計算屬性是 true 還是 false 來檢查模板,v-if然后顯示按鈕。


查看完整回答
反對 回復 2022-07-08
?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

不幸的是,我無法獲得上述任何一個答案。el 是未定義的,所以如果有人可以根據我在如何獲取 el 問題中的代碼向我解釋,那就太好了。


我做了一個不理想的工作,但它適用于我有代碼的地方updated,所以我現在就這樣做。非常感謝大家的幫助


這是我使用的代碼


   updated() {

      if ('title-data' in this.$refs) {

        const el = this.$refs['title-data']

        const heightDiff = Boolean(el.scrollHeight - el.offsetHeight > ALLOWED_HEIGHT_VARIANCE)

        if (heightDiff) {

          this.showButton = heightDiff

          el.className += ' read-more'

        }

      }

    },


查看完整回答
反對 回復 2022-07-08
  • 2 回答
  • 0 關注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號