Vant UI toast 导入报错,使用官方的方法全局导入:
import Vue from 'vue'; import { Toast } from 'vant'; Vue.use(Toast);
理应自动将$toast挂载到Vue实例下,在 vue 中可以直接使用 this.$toast('提示'),实际在本地开发中可以正常使用,构建发布后,则会提示 this.$toast is not defined。
查看源码才发现,对于组件会自动注册可以直接使用,非组件的则不会自动挂载。
那么需要自行定义,不用 use,如下:
import Vue from 'vue'; import { Toast } from 'vant'; Vue.prototype.$toast = Toast;
这时才能在 vue 中使用 this.$toast('msg')