vue3定义和使用全局变量方法,vue3中使用echarts


前言

在我们使用vue3.x的时候,有些特性还是跟vue2.x区别很大的,这里讲下echarts的使用和怎么定义和使用全局变量

vue2中是:Vue.prototype.$http = () => {}
vue3中是:const app = createApp({})
                app.config.globalProperties.$http = () => {}

一、定义和使用全局变量

1.定义

main.ts的代码:定义一个全局echarts

import { createApp } from 'vue'
import App from './App.vue'
import ElementPlus from 'element-plus';
import 'element-plus/lib/theme-chalk/index.css';
import router from './router/index';
import store from "./store/index"
import * as echarts from 'echarts';


const app = createApp(App)
app.config.globalProperties.$echarts = echarts
app.use(ElementPlus)
app.use(router)
app.use(store)
app.mount('#app')

2.使用

1.先引入getCurrentInstance

import {getCurrentInstance} from "vue";

2.实例化
这里要注意的是,这个变量名一定得是proxy,不能是像有些说的是ctx,否则的话是获取不到的,后面的括号也要加

const { proxy } = (getCurrentInstance() as any)

3.完整代码(使用echarts)

import {
  onMounted,
  ref,
  reactive,
  defineComponent,
  getCurrentInstance,
} from "vue";
export default defineComponent({
  name: "Home",
  setup() {
    const { proxy } = (getCurrentInstance() as any)

    onMounted(() => {
      let positionTop5_option = {
        barWidth: 20,
        color: "#4CA6FE",
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        xAxis: {
          type: "category",
          data: ["前端", "后端", "产品", "ui设计", "IOS开发"],
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            data: [120, 200, 150, 80, 70],
            type: "bar",
            itemStyle: {
              normal: {
                color: new proxy.$echarts.graphic.LinearGradient(
                  0, 0, 0, 1,
                  [
                    { offset: 0, color: '#7BAAF3' }, // 柱图渐变色
                    { offset: 1, color: '#9966FD' } // 柱图渐变色
                  ]
                )
              },
              emphasis: {
                color: new proxy.$echarts.graphic.LinearGradient(
                  0, 0, 0, 1,
                  [
                    { offset: 0, color: '#7BAAF3' }, // 柱图高亮渐变色
                    { offset: 1, color: '#9966FD' } // 柱图高亮渐变色
                  ]
                )
              }
            }
          },
        ],
      };
      
      let chartDom = document.getElementById("positionTop5");
      let chart1 = proxy.$echarts.init(chartDom);
      chart1.setOption(positionTop5_option);
    });

    return {};
  },
});
  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Vue 3 使用 TypeScript 定义全局方法和变量的方式与 Vue 2 有些不同。下面分别给出定义全局方法全局变量方法。 ### 定义全局方法 可以在 `src/main.ts` 文件定义全局方法,例如: ```typescript import { createApp } from 'vue'; declare module '@vue/runtime-core' { interface ComponentCustomProperties { $myGlobalMethod: () => void; } } const app = createApp(App); app.config.globalProperties.$myGlobalMethod = () => { console.log('This is a global method!'); }; app.mount('#app'); ``` 在上面的例子,我们使用 `declare module` 声明了一个模块,然后在 `ComponentCustomProperties` 接口添加了一个 `$myGlobalMethod` 属性,它是一个函数类型,不接收参数,返回值为 `void`。接着,我们在 `app.config.globalProperties` 上挂载了实现该方法的函数。 这样,在 Vue 应用程序的任意组件都可以使用 `$myGlobalMethod` 方法,例如: ```vue <template> <button @click="$myGlobalMethod()">Click me!</button> </template> ``` ### 定义全局变量 可以在 `src/main.ts` 文件定义全局变量,例如: ```typescript import { createApp } from 'vue'; declare module '@vue/runtime-core' { interface ComponentCustomProperties { $myGlobalVariable: string; } } const app = createApp(App); app.config.globalProperties.$myGlobalVariable = 'This is a global variable!'; app.mount('#app'); ``` 在上面的例子,我们同样使用 `declare module` 声明了一个模块,然后在 `ComponentCustomProperties` 接口添加了一个 `$myGlobalVariable` 属性,它是一个字符串类型。接着,我们在 `app.config.globalProperties` 上挂载了该全局变量的值。 这样,在 Vue 应用程序的任意组件都可以使用 `$myGlobalVariable` 变量,例如: ```vue <template> <p>{{ $myGlobalVariable }}</p> </template> ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值