HarmonyOS 动态import遇到错误:Failed to resolve the requested entryPoint. baseFileName?

我在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可以?

阅读 506
1 个回答

请检查oh-package.json5的dependencies里InterfaceLib的大小写 以及build-profile.json5里buildOption下packages里的InterfaceLib大小写。动态引入包名是需要区分大小写的,而 import { QuoteService } from 'InterfaceLib’的方式不用区分。

// oh-package.json5
"dependencies": {
  "InterfaceLib": "file:../InterfaceLib",
},
// build-profile.json5
"buildOption": {
  "arkOptions": {
    "runtimeOnly": {
      "sources": [
      ],
      "packages": [
      "InterfaceLib"
      ]
    },
  }
},
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进