常见问题

为什么在 SdmProvider 中 usePageEvent 监听不到页面事件?

在小程序中,页面生命周期按顺序触发:Page.onLoad → 组件渲染 → 组件内的 useEffect / usePageEvent 注册。 而 SdmProvider 为了保证设备数据就绪,会采用异步初始化(通过 getDeviceInfo / getDeviceListByDevIds 等 API)。当 SdmProvider 将渲染子组件延后,子组件可能在 Page.onLoad 之后才被挂载并注册事件处理器,导致 usePageEvent('onLoad') 永远无法收到已经触发过的 onLoad 事件。

解决方案:目前建议使用 useEffect 来 监听组件的挂载和卸载来替代页面生命周期事件的监听。

如何去掉每次 DP 点下发或上报的打印信息

在 SDM 中,为了方便开发者调试,默认会使用 interceptors 拦截器 来打印每次 DP 点的下发和上报信息,如果您想去掉这些打印,可以通过调整 Log 配置的优先级来实现,参考以下示例:

diff --git a/src/devices/index.ts b/src/devices/index.ts
index 05f6421..2ede1a1 100644
--- a/src/devices/index.ts
+++ b/src/devices/index.ts
@@ -9,6 +9,9 @@ export const dpKit = createDpKit<SmartDeviceSchema>({ protocols });
 
 const deviceOptions = {
   interceptors: dpKit.interceptors,
+  logConfig: {
+    level: 'FATAL',
+  },
 } as SmartDeviceModel<SmartDeviceSchema>['options'];
 
 /**