首頁  >  問答  >  主體

無法將屬性disable設定為按鈕

<p>如下所示的程式碼中,我正在開發一個子元件,在其中我建立了一個按鈕,並且想要新增 <code>disable</code> 屬性。 給定下面的程式碼,<code>disable</code> 屬性被紅色底線標記,並且錯誤訊息顯示:</p> <pre class="brush:php;toolbar:false;">類型 '"isDigitizePolygonDisabled"' 無法賦值給型別 'Booleanish | undefined'</pre> <p>請告訴我如何正確設定 <code>disable</code> 屬性 <strong>代碼</strong>:</p> <pre class="brush:php;toolbar:false;"><template> <button id="idDigitizePolygonBtn" class="digitizePolygonBtn" disabled='isDigitizePolygonDisabled'> <slot></slot> </button> </template> <script lang="ts"> import { ref } 從 'vue' let isDigitizePolygonDisabled = ref(true) export default { data() { return { isDigitizePolygonDisabled } }, props: { isDigitizePolygonDisabled: { type: Boolean, required: true }, } </script></pre> <p><br /></p>
P粉039633152P粉039633152417 天前468

全部回覆(1)我來回復

  • P粉376738875

    P粉3767388752023-08-17 12:21:54

    在Vue中,當您想要綁定一個布林屬性(如disabled)時,您可以使用v-bind指令(或其簡寫:)。這將一個屬性綁定到一個表達式。

    如果您嘗試以您的方式綁定disabled屬性,Vue會認為您正在嘗試將字串「isDigitizePolygonDisabled」設定為disabled的值,這是無效的。因此,您看到的錯誤。

    所以,最終的程式碼將是:

    <template>
      <button id="idDigitizePolygonBtn" class="digitizePolygonBtn" :disabled="isButtonDisabled">
        <slot></slot>
      </button>
    </template>
    
    <script lang="ts">
    import { defineComponent, ref } from 'vue'
    
    export default defineComponent({
        props: {
            isDigitizePolygonDisabled: { 
                type: Boolean,
                required: true
            },
        },
        setup(props) {
            
            // For now, just return the prop
            return {
                isButtonDisabled: props.isDigitizePolygonDisabled
            }
        }
    })
    </script>

    我更喜歡使用defineComponentsetup,我認為這更直接。

    希望對您有幫助!

    回覆
    0
  • 取消回覆