Home > Software engineering >  vue component complains about import
vue component complains about import

Time:01-06

I'm writing a vue component NavItem that represents am Item in a Navigation. I also have a NavItemModel class written in TS. But when I try to import it, vue complains that the module is not found:

Project structure

vue
./src
...../components
...../components/NavItem.vue
...../models
...../models/NavItemModel.ts

NavItem.vue

<template>
    <div>
        <a href="">{{content.title}}</a>
    </div>
</template>
<script>
import {NavItemModel}  from "../models/NavItemModel";
export default{
    name: 'NavItem',
    props:{
        content: NavItemModel
    },
    components:{
        
    }
}
</script>

NavItemModel.ts

export class NavItemModel{
    title:String;
    link: String;
    parentItem:NavItemModel;
    childItem:NavItemModel;
    constructor(title:String,link:String,parentItem:NavItemModel,childItem:NavItemModel){
        this.title = title;
        this.link = link;
        this.parentItem = parentItem;
        this.childItem = childItem;
    }
}

vue error Where am I doing wrong?

CodePudding user response:

  1. Set typescript language <script lang='ts'>

  2. Declare your property like

     props: {
      content: Object as () => NavItemModel
     }
    

CodePudding user response:

You should not destructure the class. Destructing is used for properties and functions

<template>
    <div>
        <a href="">{{content.title}}</a>
    </div>
</template>
<script>
import NavItemModel  from "../models/NavItemModel";
export default{
    name: 'NavItem',
    props:{
        content: NavItemModel
    },
    components:{
        
    }
}
</script>

  •  Tags:  
  • Related