Cannot find name error angular

Akshay

Hello I'm new to angular and I am facing an error of cannot find a name Categories. I have created a service to fetch the data for *ngFor.

Service -

import { Injectable } from '@angular/core';
import { Categories } from '../data/categories';
import { CATEGORIES } from '../data/categorydata';

@Injectable()
export class CategoriesService {

  constructor() { }

  getCategories(): Categories[] {
    return CATEGORIES;
  }
}

Category.ts file

import { subCategory } from './subcategory';

export class Categories {
  id: number;
  categoryName: string;
  subcatName: subCategory[];
}

Categorydata.ts

import { Categories } from './categories';

export const CATEGORIES: Categories[] = [
  {
    id: 1,
    categoryName: 'jeans',
    subcatName: [
        { subcategory: 'example' },
    ],
  },
];

Categories.component.ts

import { Component, OnInit } from '@angular/core';
import { CategoriesService } from '../services/categories.service';

@Component({
  selector: 'app-categories',
  templateUrl: './categories.component.html',
  styleUrls: ['./categories.component.scss']
})
export class CategoriesComponent implements OnInit {
  categoryList: Categories[];

  constructor(private categoryservice: CategoriesService) { }

  ngOnInit() {
    this.categoryList = this.categoryservice.getCategories();
  }
}

When I run ng serve. It is giving me an error

error TS2304: Cannot find name 'Categories'.

For line in categories.component.ts -

categoryList: Categories[];

Please let me know if you need additional information.

Daniel W Strimpel

You need to import the type Categories like you did in your service class so you can use the type:

import { Categories } from '../data/categories';

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

angular with typescript error, cannot find name angular

Getting Error - Cannot find name 'angular'

Angular and Typescript: Can't find names - Error: cannot find name

Cannot find name of 'of'' Angular

TypeScript error in Angular2 code: Cannot find name 'module'

Angular8 - error TS2304: Cannot find name

What causes the "Cannot find name" error in this Angular 11 application?

Angular material stepper: Error: Cannot find control with name: 'formArray'

error TS2304: Cannot find name 'trim' in Angular 5

After update webpack and Angular receives an error Cannot find name 'setImmediate'

Compile error - Cannot find name 'Map' & more - Angular 2

Angular 4 Error: Cannot find control with unspecified name attribute

Angular 2+ Error: Cannot find name 'gapi'

Angular error TS2304: Cannot find name 'config'

Angular Cannot Find Name Buffer

Angular 7: 'Cannot find name ' '

Typescript cannot find name error

Angular 6: ERROR Error: Cannot find control with unspecified name attribute with angular material and reactive forms

ERROR Error: Cannot find control with name: 'educationDetails'

ERROR Error: Cannot find control with name:

Cannot find name 'headers'. in angular 2

Angular service testing: Cannot find name 'asyncData'

Cannot find name 'Http' in Angular 10

Angular 2 Cannot find name SimpleChanges

Angular Cannot find control with unspecified name attribute

Visual Studio Code: Cannot find name angular?

Cannot find name +'Input'. any Angular 2

Angular: Cannot find control with name: 'date'

Angular 2: Cannot find name 'Subscription'