angular-如何访问在角度库的component.ts内部声明的函数?

问题描述

我正在构建一个角度库(我们将其视为“ mylib”)。在此库中,我仅使用了mylib.component.ts文件。我在该文件的模板变量中添加了html元素代码。用于修改这些html元素的函数也位于同一component.ts文件中。我成功构建了它并发布到npm。但是,当我尝试在新项目中安装并使用它时,它表示不存在此类功能。我做错了什么?我想知道的是,如何访问在角度库的component.ts内部声明的函数。如果不可能,那么在另一个项目中安装该库之后,我应该怎么做才能访问这些功能?

mylib.component.ts

H

mylib.module.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

  }
}

public.api.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 { }

这就是我试图在新项目中使用上述库的方式

app.component.html

/*
 * Public API Surface of mylib
 */

export * from './lib/mylib.service';
export * from './lib/mylib.component';
export * from './lib/mylib.module';

app.component.ts

<srr-mylib></srr-mylib>

app.module.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)"
  }
 

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)