文章

js 遍历所有vue组件

浏览: 70评论: 0发布时间: 2025-12-18
// 从根节点开始,递归打印所有组件
function logAllComponents(component) {
   // 1. 打印当前组件的名字
   if (component.$options && component.$options.name) {
       console.log("发现组件:", component.$options.name);
   }
   
   // 2. 检查是否有 WxBridge
   if (component.WxBridge) {
       console.log("  -> 这个组件有 WxBridge!");
   }
 
   // 3. 递归遍历子组件
   if (component.$children && component.$children.length > 0) {
       component.$children.forEach(child => logAllComponents(child));
   }
}
 
// 启动遍历(通常 app 挂载在 id="app" 的 DOM 上)
// 需要等页面加载完执行
var root = document.getElementById('app') ? document.getElementById('app').__vue__ : null;
if (root) {
   logAllComponents(root);
} else {
   console.log("找不到 Vue 根实例");
}