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.
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.
Comments