I’m building an angular library (let’s consider it as ‘mylib’). In this library, I have used only the mylib.component.ts file. I added the html element codes inside the template variable of this file. And the functions that are used to do modifications to those html elements are also in the same component.ts file. I successfully built it and published to npm. But when I try to install and use it in a new project, it says that such functions doesn’t exist. What have I done wrong? All I want to know is, how to access the functions declared inside component.ts of an angular library. If it's not possible, then what should I do to access these functions after installing this library in another project?
mylib.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'srr-mylib',
template: `
<h1> Random Text </h1>
`,
styles: [
]
})
export class ElapsedtimerComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
startTrans(){
//some code here
}
}
mylib.module.ts
import { NgModule } from '@angular/core';
import { MylibComponent } from './mylib.component';
import { CommonModule } from '@angular/common';
import { BrowserModule } from '@angular/platform-browser';
@NgModule({
declarations: [MylibComponent],
imports: [
CommonModule,
BrowserModule
],
exports: [MylibComponent]
})
export class MylibModule { }
public.api.ts
/*
* Public API Surface of mylib
*/
export * from './lib/mylib.service';
export * from './lib/mylib.component';
export * from './lib/mylib.module';
And this is how i tried to use the above library in a new project
app.component.html
<srr-mylib></srr-mylib>
app.component.ts
import { Component } from '@angular/core';
import { MylibModule } from '@somename/mylib'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(
private libz:MylibModule
) {
libz.startTrans() //<----- this doesn't work. it gives the error as "Property 'startTrans' does not exist on type 'MylibModule'.ts(2339)"
}
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { MylibModule } from '@somename/mylib'
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
MylibModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
@ViewChildto get the component inside your HTML. But this is an anti-pattern, you should find an other way to change the color (a common service, or an @Input in your lib's component...)document.getElementById('timer').style.fontSize = fontSize+'px';I know you can do this using an Input(). But I need to do this programmatically using a function. Cuz I need to apply the same logic for other functions as well. Those said functions doesn’t return anything. They just modify the html elements.