as shown in the below posted code, i am developing a child-component, in it i created a button and i want to add disable
property.
given the code below, the disable
property is underlined with red color and the error message says:
Type '"isDigitizePolygonDisabled"' is not assignable to type 'Booleanish | undefined'
please let me know how to set disable
property correctly
code:
<template>
<button id="idDigitizePolygonBtn" class="digitizePolygonBtn" disabled='isDigitizePolygonDisabled'>
<slot></slot>
</button>
</template>
<script lang="ts">
import { ref } from 'vue'
let isDigitizePolygonDisabled = ref(true)
export default {
data() {
return {
isDigitizePolygonDisabled
}
},
props: {
isDigitizePolygonDisabled: {
type: Boolean,
required: true
},
}
</script>
2
Answers
In Vue, when you want to bind a boolean attribute like disabled, you can use the v-bind directive (or its shorthand
:
). This binds an attribute to an expression.If you try to bind the disabled attribute the way you’re doing it, Vue thinks you’re trying to set the string ‘isDigitizePolygonDisabled’ as the value for disabled, which is invalid. Hence, the error you’re seeing.
So, final code will be:
I prefered to use
defineComponent
andsetup
which I think are more straight forward to use.Hope this helps!
Solution # 1, declare as
property
props
declaration – Vue DocsOptions API
In the Options API, you can define your component’s properties in the
props
attribute, the value of which you’ll need to pass when declaring the component. You can pre-set the property’s data type, in this case,boolean
. You can provide a default value, as in my example,true
, or you can decide to make the variable mandatory, in which case specifying a default value is not significant. For the sake of quickly illustrating my example, I had to work with a default value.How to import Children Component to Parent Element (Options API)
If the component containing the button is named
ChildrenButton
, then the declaration within the parent element would look like this:Composition API
In the Composition API, the
defineProps()
function can be used to define properties. Its structure is similar to the example from the previous Options API.defineProps()
– StackOverflow AnswerdefineProps()
– Vue DocsHow to import Children Component to Parent Element (Composition API)
If the component containing the button is named
ChildrenButton
, then the declaration within the parent element would look like this:Solution # 2, declare as
reactive variable
Instead of setting a
property
, you also have the option to declare a fixedreactive variable
. In this case, the variable’s value cannot be directly assigned from outside.Options API
In the case of the Options API, you can declare such variables within the
data()
property.data()
– Vue DocsComposition API
In the Composition API, you have access to the
reactive()
andref()
functions. The provided descriptions demonstrate their functionalities well.ref()
– Vue Docs (reactive core)reactive()
– Vue Docs (reactive core)