ant design vue3中引入message消息提示,全局引入亲测有效

两种方式

第一种:使用provide和inject方式

第二种:使用全局挂载$message方式

第一种:

//main.ts
import {
    createApp } from 'vue';
import App from './App';
import  Antd,{
    message } from 'ant-design-vue';
import 'ant-design-vue/es/message/style/css';

const app = createApp(App);
app.use(Antd);
// 在定义app的原型上用provide进行挂载
app.provide('message', message);
// xxx.vue
import {
     inject } from 'vue';
const msg: any = inject('message');
const cancel = () => {
   
    msg.info('已取消');
};

第二种:

//main.ts
import {
    createApp } from 'vue';
import App from './App';
import  Antd,{
    message } from 'ant-design-vue';
import 'ant-design-vue/es/message/style/css';

const app = createApp(App);
app.use(Antd);
app.config.globalProperties.$message = message;
import {
     getCurrentInstance } from 'vue';
const {
    proxy } = getCurrentInstance();
const cancel = () => {
   
    proxy.$message.info('已取消');
};

关于message消息提示全局配置,vue3中第二种方式可能后续就会失效,建议使用官方推荐的第一种方式注入,provide和inject搭配简直无敌,使用vite+ts+vue3项目,unplugin-vue-components插件无法处理非组件模块,如 message、Modal、notification、Icon等,这种组件需要手动加载,都可以使用这种方式解决,祝大家好运,共勉!

相关推荐

  1. Vue3 + TS 按需引入全局引入 Echarts

    2023-12-25 09:44:02       12 阅读
  2. 在html页面引入的element-plusMessage 消息提示用法

    2023-12-25 09:44:02       16 阅读
  3. Mac OSGit版本更新(有效

    2023-12-25 09:44:02       17 阅读
  4. Element UI 消息提示 Message

    2023-12-25 09:44:02       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-25 09:44:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-25 09:44:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-25 09:44:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-25 09:44:02       20 阅读

热门阅读

  1. HBase 搭建过程中常见问题

    2023-12-25 09:44:02       42 阅读
  2. sklearn所需要的知识点

    2023-12-25 09:44:02       31 阅读
  3. Spring Cloud Feign作为HTTP客户端调用远程HTTP服务

    2023-12-25 09:44:02       43 阅读
  4. 7.仿若依后端系统业务实践

    2023-12-25 09:44:02       35 阅读
  5. 解释器模式(Interpreter)

    2023-12-25 09:44:02       37 阅读
  6. centos系统部署rancher1.6版本并部署服务

    2023-12-25 09:44:02       41 阅读
  7. UOS nginx

    2023-12-25 09:44:02       32 阅读
  8. css 实现 Popover 弹出框样式

    2023-12-25 09:44:02       32 阅读