开发者问题收集

vueerror 未捕获的类型错误:无法读取未定义的属性(读取“use”)

2022-09-23
1144

我正在尝试在我的 vue 项目中添加 toastr。但是我在控制台中遇到了这个错误--

Uncaught TypeError: Cannot read properties of undefined (reading 'use')

这是我在 app.js 中的代码---

import Vue from 'vue';

import './bootstrap';
import { createApp } from 'vue';
import CreateCart from './components/CreateCart.vue';

import CxltToastr from 'cxlt-vue2-toastr';
import 'cxlt-vue2-toastr/dist/css/cxlt-vue2-toastr.css'

var toastrConfigs = {
    position: 'top right',
    showDuration: 2000,
    timeOut: 5000,
   }
Vue.use(CxltToastr, toastrConfigs)
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

const app = createApp({});
app.component('create-cart', CreateCart);

app.mount('#app');

我该如何解决这个问题?

1个回答

如果你使用的是 Vue 3,正确的做法是:

import { createApp } from "vue";    
import App from "./App.vue";
import CxltToastr from 'cxlt-vue2-toastr';

const app = createApp(App);

const toastrConfigs = {
 position: 'top right',
 showDuration: 2000,
 timeOut: 5000,
}
app.use(CxltToastr, toastrConfigs).mount('#app');

希望这对你有帮助

Br0wn
2022-12-16