Encountering a Schema error when using Components in an Angular 7 Element

I have been working on integrating various external libraries into my custom component to use in a dashboard project I'm developing. To simplify the process, I decided to create an Angular Element that includes a Line Chart, Graphic Gauge, and additional information in an overview-item.

For this example, I am utilizing ngx-charts. I have taken the ngx-charts sample number card and inserted the HTML code from the sample into my main overview-item.component.html. By adding the sample TypeScript code to overview-item.component.ts, everything is functioning correctly in my testing application.

<p>overview-item works!</p>
<ngx-charts-number-card
    [view]="view"
    [scheme]="colorScheme"
    [results]="results"
    (select)="onSelect($event)">
</ngx-charts-number-card>

This setup displays the samples on my page perfectly. Now, I am attempting to refactor this code out of the main files into reusable components. However, the components currently do not have module files.

The overview-item.component.html no longer contains the ngx-charts code, but will display the newly generated component. Even without the chart, the application still works.

<p>overview-item works!</p>
<my-number-card></my-number-card>

my-number-card.component.html

<p>my-number-card works!</p>

It's important to note that the imports and other configurations in the module and test files remain unchanged as I move the code.

overview-item.module.ts

import { CUSTOM_ELEMENTS_SCHEMA, NgModule, NO_ERRORS_SCHEMA } from '@angular/core';

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgxChartsModule } from '@swimlane/ngx-charts';
import { NumberCardModule } from '@swimlane/ngx-charts';

import { MyNumberCardComponent } from './my-number-card/my-number-card.component';

@NgModule({
    declarations: [
        MyNumberCardComponent
        OverviewItemComponent
    ],
    exports: [OverviewItemComponent],
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        NumberCardModule,
        NgxChartsModule
    ],
    schemas: [
        CUSTOM_ELEMENTS_SCHEMA,
        NO_ERRORS_SCHEMA
    ]
})
export class OverviewItemModule { }

EDIT Added my-number.component.ts

import { Component, Input, OnInit } from '@angular/core';

@Component({
    selector: 'overview-item',
    template: `
      <ngx-charts-number-card
        [view]="view"
        [scheme]="colorScheme"
        [results]="results"
        (select)="onSelect($event)">
      </ngx-charts-number-card>
    `
})
export class OverviewItemComponent implements OnInit {
    view = [700, 200];
    colorScheme = {
        domain: ['#5AA454', '#A10A28', '#C7B42C']
    };
    results = [
        { "name": 'Germany', 'value': 8940000 },
        { 'name': 'USA', 'value': 5000000 },
        { 'name': 'France', 'value': 7200000 }
    ];

    constructor() { }

    onSelect(event) {
        console.log(event);
    }

    ngOnInit() { }
}

As I add the HTML for ngx-charts into my-number-card HTML and TypeScript files, I encounter an issue.

Upon doing this, the overview-item.component.spec.ts file starts throwing an error:

Failed: Template parse errors:
Can't bind to 'view' since it isn't a known property of 'ngx-charts-number-card'.
1. If 'ngx-charts-number-card' is an Angular component and it has 'view' input, then verify that it is part of this module.
2. If 'ngx-charts-number-card' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("
  
    <ngx-charts-number-card
        [ERROR ->][view]="view"
        [scheme]="colorScheme"
        [results]="results"

This problem seems to be related to all the values that can be passed, as indicated by the square brackets.</p>

<p>Although I have added the schemas in response to the errors, they do not seem to resolve the issue. Additionally, the error pertains to the bindings for all possible properties that can be passed.</p>

<p>While I am sure I might be missing something simple, I can't seem to move past this obstacle. The code functions as intended when it is in the parent component, but creating a child component using <code>ng generate component is not producing the desired outcome.

Answer №1

When incorporating a component into your own, the view becomes a crucial property. This can be achieved by either referencing the wrapped component's view or by creating a local property named view to pass into the wrapped component.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When invoking a service repeatedly in Angular within a loop, the result is returned on the second iteration rather than the first

I've run into an issue where I'm attempting to invoke a service in Angular within a for loop and store the result in a Map. map = new Map<string, string[]>(); this.effectivitiesList = this.trimEffectivities.split(","); for (let ...

Developing a versatile table component for integration

My frontend app heavily utilizes tables in its components, so I decided to create a generic component for tables. Initially, I defined a model for each cell within the table: export class MemberTable { public content: string; public type: string; // ...

The specified "ID" type variable "$userId" is being utilized in a positional context that is anticipating a "non-null ID" type

When attempting to execute a GraphQL request using the npm package graphql-request, I am exploring the use of template literals. async getCandidate(userId: number) { const query = gql` query($userId: ID){ candidate( ...

Retrieve the Ionic storage item as a string

My issue is related to the code snippet below: this.storage.get('user') Upon execution, it returns the following object: t {__zone_symbol__state: null, __zone_symbol__value: Array(0)} I am uncertain about how to handle this object. Is there ...

Mocking a promise rejection in Jest to ensure that the calling function properly handles rejections

How can I effectively test the get function in Jest, specifically by mocking Promise rejection in localForage.getItem to test the catch block? async get<T>(key: string): Promise<T | null> { if (!key) { return Promise.reject(new Error(&apo ...

A guide on utilizing Material UI Fade for smoothly fading in a component when a text field is selected

I am facing an issue with a text field input and a helper component. My goal is to have the helper component fade in when a user focuses on the input field. The helper component is wrapped as follows: <Fade in={checked}> <DynamicHelperText lev ...

Is it possible to have an interface, function, and variable all sharing the same name in a declaration?

Is it possible to have an interface, function, and variable all with the same name? For example, I would like to define the function as follows: declare function someName(...args: any[]); someName('foo'); The interface would look like this: ...

Preventing Button Clicks in Angular 2 When Form Data is Modified

I am looking to determine if at least one field in my form has been altered. When this condition is met, the disableButton variable will be set to true, and false if there are no changes in the form. Here is the snippet of code I am currently using: // Th ...

The Next.js build encountered an error - unable to locate function in next/script module

While constructing a CMS using next.js, one of the key components is media management through Cloudinary. The integration of the Cloudinary Media Library widget was successful during development using next/script. However, an error has now emerged that pre ...

What is the best way to implement callbacks with $http in Typescript?

When making my $http call, I am looking for the most adaptable way to handle all the parameters returned in the .success and .error functions. Here is an example of what my $http call looks like: this.$http({ url: "/api/x", method: "GET" }) .success((? ...

How come TypeScript does not detect when a constant is used prior to being assigned?

There's an interesting scenario I came across where TypeScript (3.5.1) seems to approve of the code, but it throws an error as soon as it is executed. It appears that the root cause lies in the fact that value is being declared without being initiali ...

Deploy your Angular website for absolutely no cost on Firebase by leveraging the Command Line Interface

Looking to deploy an Angular application on Firebase cloud for free using the CLI? Recently started delving into Angular 9 and exploring Firebase's cloud server capabilities, I'm interested in seeing how simple it is to deploy an app on Firebase. ...

Angular - What causes variables to lose data after component changes?

Having an issue with two components - one creating and changing an array, and the other getting the array. The problem is that when retrieving the array in the second component, it only shows the default empty data. Code for array creation: export cla ...

Can anyone tell me what I might be doing incorrectly when comparing these two timestamps?

I am facing an issue while comparing two timestamps in Angular. Here is the code snippet: public isAuthenticated(): boolean { const token = localStorage.getItem('Fakelife'); const lifetime = new Date().getTime(); const result = life ...

Encountered a style group error 'non-collision' while using Angular with HERE Maps JS API 3.1

Occasionally, I encounter an error when trying to load a HERE map with the satellite base layer: Tangram [error]: Error for style group 'non-collision' for tile 13/16/15542/12554/15 Cannot read property 'retain' of undefined: TypeE ...

The combination of Object.keys() and the find function

Having trouble figuring out why I'm getting an error when attempting to use ES6 .find on the following data in order to retrieve the record with id number 3. { {id:10,title:'Dairy & Eggs'} {id:7,title:'Laundry & Household'} {id ...

Having trouble retrieving documents from a nested collection in Firebase

I am attempting to retrieve all documents from Firebase that are based on a query. Here is my current firebase structure: https://i.stack.imgur.com/tXrX8.png Even though I have two documents inside the "ListaFavorite" collection, when I check using empty ...

Injecting a useFactory provider in Angular is a common practice

I manage a factory provider service that selects a service based on a flag. Everything works fine when I need a debug students service, but when I set the flag to false, the application throws an ERROR TypeError: serverService.fetchData is not a function. ...

Angular router consistently redirecting to the identical module

It's really frustrating me. I've set up 3 basic routes in my app module: routes: Routes = [ { path: '', redirectTo: '/customers', pathMatch: 'full' }, { path: 'customers', loadChildren: './com ...

Searching for TypeScript type definitions for the @Angular libraries within Angular 2

After updating my application to Angular2 v2.0.0-rc.1, I am encountering TypeScript compile errors and warnings when webpack bundles my application. These messages appear for any @angular packages referenced in my TypeScript source files: ERROR in ./src/a ...