惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
腾讯CDC
T
Threatpost
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
大猫的无限游戏
大猫的无限游戏
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
Cisco Talos Blog
Cisco Talos Blog
C
Cisco Blogs
A
Arctic Wolf
人人都是产品经理
人人都是产品经理
NISL@THU
NISL@THU
L
LINUX DO - 热门话题
爱范儿
爱范儿
GbyAI
GbyAI
The Register - Security
The Register - Security
AWS News Blog
AWS News Blog
MyScale Blog
MyScale Blog
T
Tenable Blog
Hugging Face - Blog
Hugging Face - Blog
A
About on SuperTechFans
Cyberwarzone
Cyberwarzone
量子位
Microsoft Azure Blog
Microsoft Azure Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
The Cloudflare Blog
B
Blog RSS Feed
小众软件
小众软件
D
Docker
Know Your Adversary
Know Your Adversary
Y
Y Combinator Blog
P
Privacy & Cybersecurity Law Blog
Engineering at Meta
Engineering at Meta
Latest news
Latest news
AI
AI
SecWiki News
SecWiki News
酷 壳 – CoolShell
酷 壳 – CoolShell
S
Secure Thoughts
N
News | PayPal Newsroom
The Hacker News
The Hacker News
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
博客园 - 司徒正美
L
Lohrmann on Cybersecurity
Cloudbric
Cloudbric

博客园 - 折翼的飞鸟

React中 state值根据props传入值变化的静态方法 getDerivedStateFromProps 微信小程序分享图片显示自定义内容 Taro4.x 引入 taro-ui 组件库 Taro4.x引入redux报错:TypeError: middleware is not a function uniapp+vue3 微信小程序使用mqtt通信 Taro 在页面中import包、组件、样式文件的顺序自定义规则 Taro优化VirtualList虚拟列表组件 Taro 引入moment.js打包过大 Taro 打包体积分析 React:消息订阅(subscribe)-发布(publish)机制 Taro 插件引入 Taro: chooseLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json/ext.json Taro 支付宝小程序Page页获取小程序启动时的参数 Taro 主包vendors.js文件过大 Taro自定义Tabbar Taro 微信转支付宝小程序: 问题八 TypeError: Function(...) is not a function Taro微信转支付宝小程序:问题七 引入的文件图片文件名不能有@符号 Taro微信转支付宝小程序:问题六 同时编译微信小程序和支付宝小程序 Taro微信转支付宝小程序:问题五 编译时没有报错,工具中却莫名奇妙的错误提示
uniapp+vue3 在App.vue中如何设置全局调用方法
折翼的飞鸟 · 2025-06-15 · via 博客园 - 折翼的飞鸟

在uni-app中,如果你想在App.vuesetup函数中设置全局可调用的方法,你可以通过以下几种方式来实现:

1. 使用provideinject (vue3 推荐使用此方法

 第一步:在vue3中使用provideinject来传递方法到全局。

 在App.vue中创建全局方法:

<script setup>
  import { provide } from "vue";
  
 // 全局方法名 const globalTheme = (data) => { console.log("设置了全局方法", data); };  // 设置 provide("globalTheme", globalTheme); </script>

   第二步: 在对应的页面或者组件调用全局方法

<template>
  <View class="custom-tab-page app-page order-page" :style="themeStyle">
    <uni-row class="demo-uni-row">
      <uni-col :span="8">
        <button class="theme-btn" @click="toggleTheme('light')">白色主题</button>
      </uni-col>
      <uni-col :span="8">
        <button class="theme-btn" @click="toggleTheme('dark')">黑色主题</button>
      </uni-col>
      <uni-col :span="8">
        <button class="theme-btn" @click="toggleTheme('orange')">深红主题</button>
      </uni-col>
    </uni-row>
  </View> 
</template>

<script setup>
  import { ref, inject } from "vue"; 
  const globalTheme = inject("globalTheme");  // 注入全局方法
  const themeStyle = ref("");

  const toggleTheme = (name) => { 
    if (name == "dark") {
      themeStyle.value = `
        --primary-color: #000000; 
        --text-color: #f1f1f1;
      `;
    } else if (name == "orange") {
      themeStyle.value = `
        --primary-color: #ee6666; 
        --text-color: #3ba272;
      `;
    } else {
      themeStyle.value = `
        --primary-color: #FFFFFF; 
        --text-color: #ff0000;
      `;
    }
    // 调用全局方法
    globalTheme(name);
  };
</script>

<style scoped>
  .app-page {
    background-color: var(--primary-color);
  }
  .theme-btn {
    color: var(--text-color);
  }
</style>

 2、使用Vue 3的app.config.globalProperties (此方法我没试过,再次记录一下)

   可以在App.vue的setup函数中,通过getCurrentInstance().appContext.config.globalProperties来添加全局方法。这种方法适用于Vue 2和Vue 3

 第一步:在App.vue方法中

import { getCurrentInstance } from 'vue';
 
export default {
  setup() {
    const app = getCurrentInstance().appContext.config.globalProperties;
    app.globalMethod = () => {
      console.log('这是全局方法');
    };
  }
}

    第二步: 在其他组件中调用

export default {
  mounted() {
    this.globalMethod(); // 调用全局方法
  }
}

3. 使用Vue插件方式(适用于所有组件)

    创建一个插件,然后在main.js中全局注册这个插件,也是一种不错的方式。

    第一步: 创建插件文件(globalMethods.js)

export default {
  install(app) {
    app.config.globalProperties.globalMethod = () => {
      console.log('这是全局方法');
    };
  }
}

   第二步:在main.js 中注入插件

import { createApp } from 'vue';
import App from './App.vue';
import GlobalMethods from './plugins/globalMethods';
 
const app = createApp(App);
app.use(GlobalMethods); // 使用插件
app.mount('#app');

  第三步:在组件中调用全局方法

export default {
  mounted() {
    this.globalMethod(); // 调用全局方法
  }
}