我在hap中通过 import { QuoteService } from ‘InterfaceLib’ 来调用的时候是正常的,但尝试上述动态导入时候,遇到了错误:
[ecmascript] Failed to resolve the requested entryPoint. baseFileName : .......
QuoteService.ets:
export class QuoteService implements QuoteInterface{
serviceName: string = "QuoteService"
constructor(serviceName: string) {
this.serviceName = serviceName
}
description(): string {
return this.serviceName + " description"
}
quoteMethod(): void {
console.log('call quoteMethod')
}
InterfaceLib的Index.ets:
export { QuoteService } from './src/main/ets/InterfaceService/QuoteService'
export { QuoteInterface } from './src/main/ets/InterfaceService/QuoteInterface'
hap:
let inter = 'InterfaceLib'
import(inter).then((ns: ESObject) => {
let quoteInstance: ESObject = new ns.QuoteService();
console.log(quoteInstance.description())
})
经过调试发现:
let inter = 'interfacelib’改成小写可以成功。
import使用以下两种方式都可以
import { QuoteService } from 'InterfaceLib'
import { QuoteInterface } from 'interfacelib'
为什么动态import的时候只有小写interfacelib可以?
请检查oh-package.json5的dependencies里InterfaceLib的大小写 以及build-profile.json5里buildOption下packages里的InterfaceLib大小写。动态引入包名是需要区分大小写的,而 import { QuoteService } from 'InterfaceLib’的方式不用区分。