uni-app:利用Vue的原型对象Vue.prototype设置全局方法及其引用

一、在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
        }
    }
};

补充:涉及到异步问题

一、main.js(全局方法有请求服务器的操作)

import App from './App'
// 添加 checkPermission 方法到 Vue.prototype 上,检查权限
Vue.prototype.$checkPermission = function(username) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: getApp().globalData.position + 'Xcxuser/checkpermission',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      dataType: 'json',
      data: {
        username: username,
      },
      success: res => {
        // console.log('Server Response:', res);
        resolve(res);
      },
      fail: err => {
        console.log(err);
        reject(err);
      }
    });
  });
};




// #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

二、userlogin.vue(调用方法)

methods: {
	//权限检查
	async checkUserPermission(username) {
		  try {
			const response = await this.$checkPermission(username);
			console.log('Server Response:', response);
			this.permissionResult = response.data; // 假设服务器返回的数据在response.data中
			this.permissionChecked = true;
		  } catch (error) {
			console.error('Error checking permission:', error);
		  }
	  },
    mounted() {
        // 页面加载时调用权限检查
        this.checkUserPermission('yourUsername');
    }

}

相关推荐

  1. #Uniapp:uni-app加载全局组件方法easycom

    2024-06-07 22:42:05       41 阅读
  2. Vueuni-app区别

    2024-06-07 22:42:05       41 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-07 22:42:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-07 22:42:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-07 22:42:05       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-07 22:42:05       20 阅读

热门阅读

  1. leetcode 279.完全平方数

    2024-06-07 22:42:05       12 阅读
  2. 使用OpenCV进行简单图像分割的3个步骤

    2024-06-07 22:42:05       13 阅读
  3. ES 面试手册

    2024-06-07 22:42:05       9 阅读
  4. 2024河南高考作文ChatGPT

    2024-06-07 22:42:05       12 阅读
  5. 汽车软件单元测试分析

    2024-06-07 22:42:05       10 阅读
  6. pytest中钩子函数的使用

    2024-06-07 22:42:05       8 阅读
  7. k8s 对外发布(ingress)

    2024-06-07 22:42:05       13 阅读
  8. conda虚拟环境如何卸载pip

    2024-06-07 22:42:05       10 阅读