Can classes from an external package be imported into an Angular library within an Angular app using Openlayers?

I am currently developing an Angular library that configures an OpenLayers map as an Angular component. The component is functioning properly, but I need to access classes and constants from a package imported by the library, specifically OpenLayers.

To work around this issue, I have also installed the same package within the Angular app. However, there seems to be some compatibility issues between the two packages, even though they are using the same version. It's difficult to articulate the problem in plain text, so I will provide some pseudo code to illustrate further.

In my Angular library, I have the following method:

import { Feature} from "ol";
....

public addFeature(feature: Feature) {
    this.layer.getSource().addFeature(feature);
}

And in my Angular app, I have the following snippet:

import { Feature} from "ol";

public test() {
    const feature = new Feature();
    myLibService.addFeature(feature)
}

However, when I run this, I encounter the following error:

Argument of type 'Feature' is not assignable to parameter of type 'Feature'. Types of property 'on' are incompatible.

This issue can be resolved by changing the type of Feature in the library function "addFeature" to type any, which is something I would prefer to avoid. Creating a new Feature() inside my library works without any problems.

My Desired Outcome Ideally, I would like to import "Feature" from the "ol" package installed within my-lib directly into my Angular app. Is there a way to achieve this?

Etc:

import { Feature} from 'my-lib/node_modules/ol';

If you need further clarification or additional information, please feel free to ask. The provided code is purely pseudo-code, but conveys the same details as the actual implementation.

Thank you in advance!

I have attempted to install the "ol" (OpenLayers) package in both my Angular app and Angular library, with both using the same version.

Answer №1

Today, I successfully tackled this issue by incorporating openLayers as a peerDependency in my Angular library and then installing it normally in the consuming app.

"peerDependencies": {
"@types/proj4": "^2.5.2",
"@types/ol": "^6.5.3",
"ol": "^7.2.3-dev.1676300784989",
"proj4": "^2.8.0" }

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

Exploring Typescript code through a practical example with reference to Uniswap's implementation

On the Uniswap website, I came across some code on the Swap page that caught my attention. You can find the code snippet in question at line 115 of the Uniswap GitHub repository. const { trade: { state: tradeState, trade }, allowedSlippage, cur ...

Angular version 16+ is incompatible with Three.js and does not function properly together

I am attempting to integrate three.js into my Angular 16 project and encountering the following error message: npm i @angular-three/core npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: [email&# ...

Having trouble extracting information from a JSON link to populate an Angular Material Table

I have successfully implemented the Angular Material Table to display data from a List. Now, I want to fetch data from a JSON URL and populate this list with that data. I've attempted several methods found online to parse the data into the list, but ...

Form with checkboxes in a Next.js and Typescript application

I am currently working on a project using Next.js and Typescript. I have implemented a form in this project, which is my first experience with Typescript and checkbox types. However, I am encountering difficulties in retrieving all checkbox values, adding ...

Halt the flow of a pipe midway through

Within my Angular application, there exists a token refresh interceptor designed to catch 401 Unauthorized errors. The interceptor then attempts to refresh an access token and resubmit the original HTTP request. There are scenarios where the token refresh ...

Guide to displaying loading progress during server response delay in React with TypeScript

I need to find a way to update the loading state to false once the server responds. The challenge is that the response occurs in one component, while the progress bar is located in another. To illustrate the scenario: const Form: React.FC = () => { ...

An effective method to utilize .map and .reduce for object manipulation resulting in a newly modified map

Here's an example of what the object looks like: informations = { addresses: { 0: {phone: 0}, 1: {phone: 1}, 2: {phone: 2}, 3: {phone: 3}, 4: {phone: 4}, 5: {phone: 5}, }, names: { 0 ...

Complicated nestjs application successfully resolves path issue

Currently, I am in the process of creating a boilerplate for NestJS and microservices (still a work in progress). The problem arises when trying to run the app or perform tests, resulting in this https://i.sstatic.net/uj4un.png You can find the code on t ...

Error in main.ts due to issues with importing components using an index.ts file

I am facing a common exception: Unexpected directive value 'undefined' on the View of component 'AppComponent' Many solutions I found online did not address my specific issue related to circular dependencies or missing export statem ...

After installing Microsoft.AspNetCore.SpaTemplates::*, the Angular template seems to be missing

Today I decided to start using .Net and successfully installed the SDK. Following instructions, I ran the CLI command to install the SPA template: dotnet new --install Microsoft.AspNetCore.SpaTemplates::* Although the command ran without any errors, I co ...

Retrieving data from a service and passing it between components in Angular 7

I have a variable called testgetinfos that holds an array of objects returned from a service. I need to access this variable in another component. Here is the function where I am retrieving the object array from a service. When I print the output of the t ...

What is the best way to manage errors and responses before passing them on to the subscriber when using rxjs lastValueFrom with the pipe operator and take(1

I'm seeking advice on the following code snippet: async getItemById(idParam: string): Promise<any> { return await lastValueFrom<any>(this.http.get('http://localhost:3000/api/item?id=' + idParam).pipe(take(1))) } What is the ...

What is the process of extracting an observable from another observable using the pipe method?

Is there a more efficient way to convert an Observable of Observables into an array of Observables in my pipe method? Here is the scenario: // The type of "observables" is Observable<Observable<MyType>[]> const observables = this.http.get<M ...

Retrieve the package.json file for a specific package by making an HTTP request to public repositories

I’ve been searching online but haven’t found a satisfying answer to my question yet. My main objective is to generate a dependency tree for a particular npmjs library of a specific version, like retrieving the dependency tree for the angular library v ...

Creating a Custom 404 Page with No Navigation Bar

Currently, I am focused on routing and navigation in my project. However, I have encountered an issue regarding how to display the 404 page without including the navigation bar and page title. Here is a snippet of my code from app.component.html: <h2&g ...

Troubles Arising from Using Mongoose's $push Function

Having a bit of trouble with my code and could use an extra set of eyes. I'm utilizing the Mongoose npm package to create a new entry in my MongoDB, which is then used in some functions within the Async npm package. The problem I'm encountering ...

Is there a way to merge two observables into one observable when returning them?

I'm struggling with getting a function to properly return. There's a condition where I want it to return an Observable, and another condition where I'd like it to return the combined results of two observables. Here is an example. getSearc ...

What steps can I take to avoid an invalid operation on a potentially null reference in typescript?

Take a look at this scenario where the variable a can potentially be null, and is explicitly defined as such. Even when strict null checks are enabled, TypeScript does not flag a possible issue in this situation - let a: string | null = "hello" function ...

Upon running the code, no errors appear on the console. However, my project isn't functioning properly and I'm encountering errors on the web console

ReferenceError: require is not defined when trying to access external "url" at Object.url in webpack_require (bootstrap:83) at client:6 importing from webpack-dev-server client index.js(http://0.0.0.0:0) vendor.js:219506 dynamically imp ...

Mapping JSON to interface in Angular 7: A step-by-step guide

I am currently working with angular7 and I have a requirement to map a json object to my interface. My goal is to create a function that can accurately map the fields of the json object to the corresponding properties in the interface. Additionally, if the ...