# 页签 TabLayout

# 页签切换

在进行页签切换成功后,会调用postMessage方法。可对message事件进行监听,并通过页签切换对应的type来执行自定义操作。

  1. postMessage方法一共发送两个参数,typedata
  2. type类型为string,根据此参数区分相应的事件。data类型为object,有两个字段fromto,from类型为string,表示来源的路由,to类型为string,表示前往的路由。

以下为示例代码:

// 框架发送message
window.postMessage( { type: "afterTabSwitch", data: { to: to, from: from } }, "*");

// 业务示例代码
// 若在子iframe里,则此处需要使用window.top.addEventListener
window.addEventListener("message", (e) => {
  const { type, data } = e.data;
  if (type === "afterTabSwitch") {
    if (data?.to === "targetRoute") {
      myMethods();
    }
  }
});