angular - 如何访问 angular 库的 component.ts 内声明的函数?
angular - How to access functions declared inside component.ts of an angular library?
我正在构建一个 angular 库(我们将其视为“mylib”)。在这个库中,我只使用了 mylib.component.ts 文件。我在这个文件的模板变量中添加了 html 元素代码。用于修改那些 html 元素的函数也在同一个 component.ts 文件中。我成功构建了它并发布到 npm。但是当我尝试在新项目中安装和使用它时,它说这些功能不存在。我做错了什么?我只想知道如何访问 angular 库的 component.ts 中声明的函数。如果不行,那么在另一个项目中安装这个库后,我应该如何访问这些功能?
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';
这就是我尝试在新项目中使用上述库的方式
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 { }
在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
}
}
app.component.html
<srr-mylib [(inputFontSize)]="myLibFontSize" (inputFontSizeChange)="onFontSizeChange($event)"></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 {
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);
}
}
我正在构建一个 angular 库(我们将其视为“mylib”)。在这个库中,我只使用了 mylib.component.ts 文件。我在这个文件的模板变量中添加了 html 元素代码。用于修改那些 html 元素的函数也在同一个 component.ts 文件中。我成功构建了它并发布到 npm。但是当我尝试在新项目中安装和使用它时,它说这些功能不存在。我做错了什么?我只想知道如何访问 angular 库的 component.ts 中声明的函数。如果不行,那么在另一个项目中安装这个库后,我应该如何访问这些功能?
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';
这就是我尝试在新项目中使用上述库的方式
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 { }
在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
}
}
app.component.html
<srr-mylib [(inputFontSize)]="myLibFontSize" (inputFontSizeChange)="onFontSizeChange($event)"></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 {
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);
}
}