angular-如何访问angular库的component.ts中声明的函数? 我正在建造一个角度图书馆(我们把它看作“MyLIB”)。在这个库中,我只使用了mylib.component.ts文件。我在这个文件的模板变量中添加了html元素代码。用于修改这些html元素的函数也在同一个component.ts文件中。我成功地构建了它并发布到npm。但是当我尝试在一个新项目中安装和使用它时,它说这样的功能不存在。我做错了什么?我想知道的是,如何访问angular库的component.ts中声明的函数。如果不可能,那么在另一个项目中安装此库后,我应该如何访问这些函数
mylib.component.tsangular-如何访问angular库的component.ts中声明的函数? 我正在建造一个角度图书馆(我们把它看作“MyLIB”)。在这个库中,我只使用了mylib.component.ts文件。我在这个文件的模板变量中添加了html元素代码。用于修改这些html元素的函数也在同一个component.ts文件中。我成功地构建了它并发布到npm。但是当我尝试在一个新项目中安装和使用它时,它说这样的功能不存在。我做错了什么?我想知道的是,如何访问angular库的component.ts中声明的函数。如果不可能,那么在另一个项目中安装此库后,我应该如何访问这些函数,angular,typescript,angular-library,Angular,Typescript,Angular Library,mylib.component.ts import { Component, OnInit } from '@angular/core'; @Component({ selector: 'srr-mylib', template: ` <h1> Random Text </h1> `, styles: [ ] }) export class ElapsedtimerComponent implements OnInit { cons
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
}
}
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 Surface of mylib
*/
export * from './lib/mylib.service';
export * from './lib/mylib.component';
export * from './lib/mylib.module';
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)"
}
}
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 { }
public.api.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
}
}
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 Surface of mylib
*/
export * from './lib/mylib.service';
export * from './lib/mylib.component';
export * from './lib/mylib.module';
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)"
}
}
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 { }
这就是我在一个新项目中使用上述库的方式
app.component.html
<srr-mylib></srr-mylib>
在Angular中,组件不应直接调用其他组件的方法。因此,根据我对您尝试执行的操作的理解,我将使用@Input,它会触发函数调用: mylib.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'srr-mylib',
template: `
<h1 [ngStyle]="{'font-size': myFontSize}"> Random Text </h1>
<button type="button" (click)="onClickPlusFontSize()">Larger font size</button>
`,
styles: [
]
})
export class ElapsedtimerComponent implements OnInit, OnChanges {
@Input() inputFontSize: number;
@Output() inputFontSizeChange: EventEmitter<number> = EventEmitter();
public myFontSize: string;
constructor() {
}
ngOnInit(): void {
}
ngOnChanges(changes: SimpleChanges): void {
if (changes.inputFontSize) {
// triggered every time the @Input value changes
this.onFontSizeChange(changes.inputFontSize.currentValue);
}
}
onFontSizeChange(newFontSize) {
this.myFontSize = `${newFontSize}px`;
// some other stuff
}
onClickPlusFontSize() {
this.inputFontSize++; // updates the value
onFontSizeChange(this.inputFontSize); // triggers the function that should be called, as if the change came from the parent
this.inputFontSizeChange.emit(this.inputFontSize) // tells the parent that the value changed
}
}
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 {
public myLibFontSize: number;
constructor() {
this.myLibFontSize = 12; // will trigger ngOnChanges on myLib
}
onSpecificEvent() {
// do stuff
this.myLibFontSize = 20; // will also trigger ngOnChanges on myLib
}
onFontSizeChange(newFontSize: number) {
// this method is optionnal.
// with '[(inputFontSize)]="myLibFontSize"' in the HTML, the value of 'myLibFontSize' will already be updated automatically.
// this function is only useful if the parent wants to do specific work when the value changes.
// so there is absolutely no need to do "this.myLibFontSize = newFontSize" here.
// the only thing to know, is that you have no clue of either 'myLibFontSize' will be updated after of before 'onFontSizeChange' is called.
// So always use 'newFontSize' in this method, to be sure of the value used.
console.log('myLibFontSize value changed !', newFontSize);
}
}
永远不要在构造函数中插入模块。您应该尝试使用
@ViewChild
在HTML中获取组件。但这是一种反模式,您应该找到另一种方法来更改颜色(公共服务,或库组件中的@Input…)@Random No-factly-changeColour不是实际的函数。我也不打算改变颜色。我只是以这个函数名为例。我只想知道如何访问库的component.ts文件中声明的函数方法“changeColor”的内容与我所说的无关。那么你能给出一个函数的真实例子吗?该函数是否返回某些内容?函数对组件本身有影响吗?@Random有很多函数。现在让我们考虑一个函数,它改变了“模板”中某个HTML元素的字体大小。下面是该函数中的代码document.getElementById('timer').style.fontSize=fontSize+'px'代码>我知道您可以使用Input()完成此操作。但是我需要使用一个函数以编程的方式来实现这一点。因为我也需要对其他函数应用相同的逻辑。这些函数不会返回任何内容。他们只是修改html元素。谢谢支持!假设html元素中有一个计数器,可以使用上面和下面的按钮更改它的值。如果我编写一个函数来返回该计数器的当前值,那么当我使用库时如何访问该函数?@SankhaRathnayake为此,您可以使用“双向绑定”,请参阅我的编辑