main.ts中注册
import { createSSRApp } from "vue";
import App from "./App.vue";
//@ts-ignore
import GlobalMessage from "@/componets/GlobalMessage/GlobalMessage.js";
function registerGlobal(app) {
app.config.globalProperties.$myMethod = function () {
console.log('This is my method.')
}
app.use(GlobalMessage);
console.error("全局", app)
}
export function createApp() {
const app = createSSRApp(App);
registerGlobal(app);
return {
app,
};
}
使用
console.log("myMethod()", $myMethod())
报错
$myMethod is not defined
1 个回复
blackhole7878
//导入getCurrentInstance
import {getCurrentInstance} from 'vue'
//获取proxy 对象(相当与vue2的this)
let { proxy } = getCurrentInstance() as any
//调用
proxy.$myMethod()