Skip to content

Instantly share code, notes, and snippets.

@gooqiao
Last active August 24, 2020 09:44
Show Gist options
  • Save gooqiao/52acc59d94941a18ee3b1b1e4d267940 to your computer and use it in GitHub Desktop.
Save gooqiao/52acc59d94941a18ee3b1b1e4d267940 to your computer and use it in GitHub Desktop.
开发时拿到所有vue组件实例, 代替vue devtools
import Vue, { VNode, RenderContext, CreateElement } from "vue";
declare global {
interface VueComponents {
[key: string]:
| Vue
| ((createElement: CreateElement, hack: RenderContext) => VNode)
| undefined;
}
interface Window {
vi: VueComponents;
}
}
interface VueInstanceRoot extends Vue {
vi?: VueComponents;
}
if (process.env.NODE_ENV !== "production") {
window.vi = {};
const GlobalMixin = Vue.extend({
mounted() {
const name = this.$options.name;
if (!name || name.startsWith("El") || name === "transition") {
return;
}
if (this.$el.className?.includes("el-")) {
return;
}
window.vi[name] = this;
if (window.__POWERED_BY_QIANKUN__) {
const root = this.$root as VueInstanceRoot;
if (!root.vi) {
root.vi = {};
}
root.vi[name] = this;
console.log(name, this);
}
window.vi[`${name}Render`] = this.$options.render;
},
});
Vue.mixin(GlobalMixin);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment