2 回答

TA貢獻1811條經驗 獲得超4個贊
您可以創建一個計算屬性來檢查文本的長度是否超過給定的數字。
computed: {
isTextLengthLongEnough() {
if(el.offsetHeight > 150) {
this.showButton = true;
}
}
然后,您可以使用計算屬性是 true 還是 false 來檢查模板,v-if然后顯示按鈕。

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'
}
}
},
添加回答
舉報