Import an instance of a class into an Angular component

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 พ.ย. 2024
  • Hello everyone! I hope this video has helped solve your questions and issues. This video is shared because a solution has been found for the question/problem. I create videos for questions that have solutions. If you have any other issues, feel free to reach out to me on Instagram: / ky.emrah
    Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!Import an instance of a class into an Angular component
    I know this is a very basic question. I have a file dinosaur.model.ts where I define an interface, and I also want to define a template
    dinosaur.model.ts
    export interface Dinosaur {
    height: number;
    weight: number;
    lifespan: number;
    diet: string;
    // and 70 other features
    export interface Dinosaur {
    height: number;
    weight: number;
    lifespan: number;
    diet: string;
    // and 70 other features
    In the same file, I'd like to be able to instantiate an object of the Dinosaur type, and export it as well
    export class customDefaults {
    herbivoreTemplate: Dinosaur = {
    height: 21;
    weight: 6.7;
    lifespan: 65;
    // 71 other features
    }
    carnivoreTemplate: Dinosaur = {
    height: 8;
    weight: 1.7;
    lifespan: 52;
    // 71 other features
    }
    export class customDefaults {
    herbivoreTemplate: Dinosaur = {
    height: 21;
    weight: 6.7;
    lifespan: 65;
    // 71 other features
    }
    carnivoreTemplate: Dinosaur = {
    height: 8;
    weight: 1.7;
    lifespan: 52;
    // 71 other features
    }
    What I need to do is to import the specific instances of my templates into a component, and I know this should be pretty simple, but I cannot make it work. In my-component.ts I'm trying:
    my-component.ts
    constructor(
    initialHerbivore: Dinosaur,
    ) {
    initialHerbivore = new customDefaults().herbivoreTemplate;
    constructor(
    initialHerbivore: Dinosaur,
    ) {
    initialHerbivore = new customDefaults().herbivoreTemplate;
    I've also tried
    export class herbivoreTemplate {
    height: number = 21;
    weight: number = 65;
    // etc.
    constructor(@Inject(Object) obj: Dinosaur) {
    Object.assign(this, obj);
    }
    }
    export class herbivoreTemplate {
    height: number = 21;
    weight: number = 65;
    // etc.
    constructor(@Inject(Object) obj: Dinosaur) {
    Object.assign(this, obj);
    }
    }
    But there's always an error, whatever I try, and I'm going round in circles. The templates are long, and I need to call them frequently, and I think using an interface for the type, and creating the object instances in the dinosaur.model.ts file is the best approach. I don't want to use a service for this, because I've already got plenty of services running in the background.
    dinosaur.model.ts
    Tags: angular,typescript,classSource of the question:
    stackoverflow....
    Question and source license information:
    meta.stackexch...
    stackoverflow....

ความคิดเห็น •