一、在main.js中设置方法checkPermission绑定到Vue.prototype
核心代码
Vue.prototype.$checkPermission = function(username) {console.log('Checking permission for:', username);
};
完整代码
import App from './App'// 添加 checkPermission 方法到 Vue.prototype 上,检查权限(这一段是方法的设置)
Vue.prototype.$checkPermission = function(username) {console.log('Checking permission for:', username);
};// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({...App
})app.$mount()
// #endif// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {const app = createSSRApp(App)return {app}
}
// #endif
二、在页面引用全局方法
核心代码
this.$checkPermission(this.username);
完整代码
export default {data() {return {username: 'testuser'};},methods: {onLoad() {this.$checkPermission(this.username);//调用方法,传递参数username}}
};