I am pretty new to typescript. Now, I have setup nuxt(bridge with typescript) app and created a component named menuIcon as such;
<div class="container">
<div class="menu-icon" :class="active ? 'change' : ''" @click="toggle">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
<div
class="overlay"
:class="active ? 'overlay-slide-right' : 'overlay-slide-left'"
@click="toggle"
id="overlay"
>
<!-- html contents here -->
</div>
</div>
</template>
<script lang="ts">
export default {
data(){
const active:boolean=false;
return{
active
}
},
methods: {
toggle():void{
this.active= !this.active
},
},
};
</script>
<style scoped>
/*css contents here*/
}
</style>
It is working in browser but I am seeing the following error in my terminal.
TS2339: Property 'active' does not exist on type '{ toggle(): void; }'.
41 | methods: {
42 | toggle():void{
> 43 | this.active= !this.active
| ^^^^^^
44 | },
45 | },
46 | };
ERROR in components/MenuIcon.vue:43:27
TS2339: Property 'active' does not exist on type '{ toggle(): void; }'.
41 | methods: {
42 | toggle():void{
> 43 | this.active= !this.active
| ^^^^^^
44 | },
45 | },
46 | };
Apparently it's something about typing but I am not sure how to fix it. I would very much appreciate anyone to shed light on me about the issues. Thanks in advance.
The component definition needs to be declared with Vue's
defineComponent
to enable TypeScript support. Nuxt Bridge recommends usingdefineComponent
from the#app
import, although you could also import it from@vue/composition-api
. Using the#app
alias is preferrable because it includes Nuxt specific types from@nuxtjs/composition-api
.Example:
The
#app
alias is normally setup in.nuxt/tsconfig.json
, but I found it's necessary to re-declare it in the root app'stsconfig.json
to work in VS Code:demo