Unable to associate with 'paint' as it is not a recognized attribute of 'mgl-layer' while using mapbox in Angular 9

I am currently working on an Angular 9 project with the latest version of mapbox integrated. My goal is to toggle between displaying contours and museums on the map.

To achieve this, I have installed the package: "@types/mapbox-gl": "^1.12.5",

and added it to app.module.ts as follows:

 NgxMapboxGLModule.withConfig({
      accessToken:
        'pk.ljljlkjnA5ZzAyYnVtaGkifQ.LfTgQzPszKiyVQjKiUFsyg', // accessToken can also be set per map (accessToken input of mgl-map)
    }),

In addition, I have created a component with the following structure:


@Component({
  template: `
    <mgl-map
      style="mapbox://styles/mapbox/streets-v9"
      [zoom]="[15]"
      [center]="[-71.97722138410576, -13.517379300798098]"
    >
      <mgl-vector-source id="museums" url="mapbox://mapbox.2opop9hr"> </mgl-vector-source>
      <mgl-vector-source id="contours" url="mapbox://mapbox.mapbox-terrain-v2"> </mgl-vector-source>
      <mgl-layer
        id="museums"
        type="circle"
        source="museums"
        [layout]="layouts.museums"
        [paint]="{
          'circle-radius': 8,
          'circle-color': 'rgba(55,148,179,1)'
        }"
        sourceLayer="museum-cusco"
      >
      </mgl-layer>
      <mgl-layer
        id="contours"
        type="line"
        source="contours"
        [layout]="layouts.contours"
        [paint]="{
          'line-color': '#877b59',
          'line-width': 1
        }"
        sourceLayer="contour"
      >
      </mgl-layer>
    </mgl-map>
    <div class="menu">
     <!--  <mat-button-toggle [checked]="true" value="contours" (change)="toggleLayer($event)"
        >contours</mat-button-toggle
      >
      <mat-button-toggle [checked]="true" value="museums" (change)="toggleLayer($event)"
        >museums</mat-button-toggle -->
      >
    </div>
  `,
  styleUrls: ['./toggle-layer.component.scss'],
})
export class ToggleLayersComponent implements OnInit {
  layouts = {
    contours: {
      visibility: 'visible',
      'line-join': 'round',
      'line-cap': 'round',
    },
    museums: {
      visibility: 'visible',
    },
  };

  ngOnInit() {}

  toggleLayer(evt: {value: 'contours' | 'museums'}) {
    const key = evt.value as 'contours';

    this.layouts[key] = {
      ...this.layouts[key],
      visibility: this.layouts[key].visibility === 'visible' ? 'none' : 'visible',
    };
  }
}

However, I encountered errors related to the 'mgl-layer' element. It seems that Angular is having trouble recognizing it within the module setup. How should I proceed?

Despite some issues, I noticed that the code editor provides suggestions for elements like [layout], indicating that the syntax is correct. But when attempting to build the Angular app, errors persist.

After making some adjustments to the component's template, I continued to face errors related to 'mgl-layer' not being recognized. In my child module declaration, everything appears to be in order. What could be causing this issue?

I made further modifications to include the 'ToggleLayersComponent' in the declarations of my main app module. Although no errors are displayed, the expected content is still not visible on the map. What else should I consider?

Answer №1

Attempting to assign the variable paint locally, but in reality providing an inline object literal. To resolve this issue, you can either create a public property within your component:

// toggle-layer.component.ts
public colorConfig = {
  'circle-radius': 8,
  'circle-color': 'rgba(55,148,179,1)'
}
// HTML template
<mgl-layer
  ...
  [paint]="colorConfig"
>...</mgl-layer>

Alternatively, you can eliminate the binding and directly use an object literal:

<mgl-layer
  ...
  paint="{
    'line-color': '#877b59',
    'line-width': 1
  }"
>...</mgl-layer>

Answer №2

@NgModule({
  declarations: [DesktopDashboardComponent, BarChartComponent, TestDesktopComponent, ToggleLayersComponent],
  imports: [
    CommonModule,
    SharedModule,
    DragDropModule,
    MatCardModule,
    MatGridListModule,
    DesktopRoutingModule,
    NgxMapboxGLModule,
    /*  MatButtonToggleModule, */
  ],
  exports: [],
})
export class DesktopDashboardModule {}

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

Problem with the functionality of the "toggled" attribute in redux-form-material-ui

Currently, I am attempting to implement Toggle functionality from redux-form-material-ui: import { Toggle } from 'redux-form-material-ui' When the toggle value changes, it successfully updates in the store upon onChange: <Col xs='3&apo ...

What is the process for accessing and updating values within nested form fields in angular2?

I have been working on an Angular 2 application that utilizes Angular reactive forms. I need to retrieve the value of the innermost element and then update that value accordingly. form : FormGroup; constructor(private formBuilder: FormBuilder) { this. ...

Implementing a dynamic listbox feature in JSP

I have a setup with two listboxes on my JSP page. The first listbox is initially populated with data from the database. When a user selects an item in the first listbox, I want the second listbox to be filled with corresponding database data using Ajax. Si ...

Oops! An issue occurred during the `ng build` command, indicating a ReferenceError with the message "Buffer is not defined

I'm facing an issue while trying to utilize Buffer in my angular component ts for encoding the Authorization string. Even after attempting npm i @types/node and adding "node" to types field in tsconfig.json, it still doesn't compile with ng buil ...

Tips for successfully passing multiple properties to a function in React

<DeleteForeverIcon className={classes.deleteHwIcon} onClick={() => { deleteHomework(value.name, value.class); }} /> I'm looking to modify the function deleteHomework so that it can receive two properties instead of just one. In add ...

What is a simple method to convert TypeScript to JavaScript?

Is it possible to eliminate TypeScript-specific keywords from a JavaScript file without using the tsc command, while ensuring that the file remains readable by humans and maintains JSX syntax? ...

How come my links aren't initiating jQuery click events?

Today, I decided to experiment with jQuery and encountered an issue. On my webpage, there are multiple links displayed in the format shown below: <a class="a_link" id="a_id<#>" href="#">Click me</a> The value <#> is a number gener ...

I'm a complete programming newbie and I want to start learning JavaScript, jQuery, and other programming languages. Where should I

Coming from a design background with zero programming knowledge, I have recently learned XHTML and CSS. Now, I am eager to expand my skills by mastering JavaScript, jQuery, and more. Where should I begin? This will be my first foray into programming. Whil ...

Enhance the functionality of jQuery sortable by including additional details

I have a li list that I have implemented sortable functionality using jQuery. In order to ensure that the updated data is sent to the correct destination, I need to include some hidden values in the serialized data. How can I achieve this? HTML <ul i ...

Utilize NodeJS API to convert a base64 data string into a downloadable PDF file

My NodeJS API is set up to communicate with another API and fetch a data object in the form of a Base64 string. The client making the API call needs to be able to download a PDF file generated from this base64 data. What is the best way to return the dat ...

Ensure Angular 15 form does not submit until password meets all validation criteria

Currently teaching myself Angular (started with version 15) by following a YouTube tutorial on creating a registration and login system. Everything is working well so far, but I'm now looking to make some adjustments and unsure about the best approach ...

Angular 7 and Spring 5 are being hindered by CORS restrictions

I'm currently working on a project that involves Spring 5 with Spring Security and Angular 7. I am facing an issue while trying to connect the frontend, receiving the following error message. It's worth mentioning that the backend and frontend pr ...

Transforming a CSV document into a JSON format in order to generate a hierarchical tree structure for constructing a D3 categorical tree diagram

I have a CSV file that is structured like this: "","Sequence","Paths","sequence_length" "1","Social -> Social -> Social -> Social -> Social -> Social -> Social -> Social",29,8 "2","Social -> Social -> Social -> Social -> S ...

Creating interactive avatars with Material UI and React to provide a dynamic user

I have developed a simple form validation application using react.js. Each user has a unique profile containing their personal information. I am looking to utilize the first letter of the user's name, for example Peter, where the letter "P" would be d ...

Displaying particles in system

I'm utilizing the Three.js ParticleSystem to showcase a large number of points, which greatly improves performance. At certain zoom levels, the particles may appear very close together, leading to the appearance of strange De Moivre fringes when adju ...

Simultaneously sending jQuery.ajax data while submitting a form

I'm facing a bit of a dilemma here. I have a form with multiple fields, including one for entering links. When you input a link and click the add button, the link is added to a link_array using jQuery. My goal is to send this array through the jQuery. ...

What is the best way to handle multi-dimensional JSON data without keys in JavaScript?

My JSON data is structured as follows: { "bitcoin": [ "-0.47", "-0.46", "-0.42" ], "maker": [ "8.29", "8.29", "6.89" ] } I want to extract values from this data where keys are not specified. How can I achieve this? Update: Tha ...

How to create a donut chart in Highcharts without an inner pie section?

I've been scouring the internet in search of a solution to create a basic donut chart using the Highcharts library. Most examples I come across show donut charts with both an inner pie and outer donut (see here). Is there a way to remove the inner pi ...

"Retrieving Data Using jQuery's .ajax Method in Visual Basic

<WebMethod()> Public Shared Function gtet() As String ... Dim GET = uClass.GetSets(dbuser, dbparam1) ... End Function and $(document).ready(function () { data = { }; var jsondata = $.toJSON(data); $.ajax({ type: "GET ...

How to Properly Initialize a Variable for Future Use in a Component?

After initializing my component, certain variables remain unassigned until a later point. I am seeking a way to utilize these variables beyond the initialization process, but I am unsure of how to do so. Below is my attempted code snippet, which throws a ...