报错
Imports of unmocked HSP modules are not allowed in pages or components. (no-page-import-unmocked-hsp)
Rule Details
HSP modules must be mocked so that the interfaces in them can be properly initialized in the Previewer. If a page or component has any unmocked HSP module imported, it may not be rendered correctly in the preview.
Examples
Examples of incorrect code for this rule:
import { add } from 'library'; // The module is not mocked.
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
Row() {
Text(this.message)
.onClick(() => add(1, 2))
}
}
}
Examples of correct code for this rule:
import { add } from 'library'; // The module is mocked. See the mocking configuration below.
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
Row() {
Text(this.message)
.onClick(() => add(1, 2))
}
}
}
Custom mocking configuration:
// src/mock/mock-config.json5
{
"library": {
"source": "src/mock/myhsp.mock.ets"
},
}
// src/mock/myhsp.mock.ets
export function add(a: number, b: number): number {
return a + b;
}
关键点是配置mock --> entry/src/mock/mock-config.json5
//为某个page配置预览VIew, 这个不配置则会导致无法预览, LazyForEachPage.ets你那个地方引入了common(这是我的hsp)就是那个类名地址
"common": {
"source": "src/main/ets/pages/LazyForEachPage.ets"
}
提一点记录一下>>>>>引入hsp
image.png