Laravel 5.7使用Vue插件Simplert报错:未知自定义元素<simplert>
Hey there! Let's break down your issue and fix it step by step.
First, let's clear up the VSCode warning you're seeing: it's not the root cause of your component registration error. That message just means the vue2-simplert-plugin doesn't include TypeScript declaration files, so VSCode can't infer types for it. It's purely an editor-level warning and won't stop the plugin from working. If you want to get rid of it, you can create a simple declaration file in your project root:
// vue2-simplert-plugin.d.ts declare module 'vue2-simplert-plugin';
Now, onto the real problem: the <simplert> element not being recognized. Here are the most likely fixes:
1. Verify Your Vue Instance's Mount Scope
Make sure the <simplert> tag in your home.blade.php is inside the DOM element your Vue instance is mounted to. Your Vue instance targets #app, so your main layout (like app.blade.php) should wrap the content section like this:
<div id="app"> @yield('content') </div>
If <simplert> is outside this #app div, Vue can't process it, hence the "unknown custom element" error.
2. Double-Check Plugin Registration Order
Your app.js looks correct in terms of order (import → register plugin → create Vue instance), but let's confirm you're not missing anything. Try explicitly registering the <simplert> component globally alongside the plugin:
require('./bootstrap'); window.Vue = require('vue'); import Simplert from 'vue2-simplert-plugin'; import { SimplertComponent } from 'vue2-simplert-plugin'; // Import the component directly require('vue2-simplert-plugin/dist/vue2-simplert-plugin.css'); Vue.use(Simplert); // Registers the $Simplert instance method Vue.component('simplert', SimplertComponent); // Explicitly register the component const app = new Vue({ el: '#app', data: { obj: { title: 'Alert Title', message: 'Alert Message', type: 'info', useConfirmBtn: true, customConfirmBtnText: 'OK' } }, methods: { openSimplert () { this.$Simplert.open(this.obj); }, closeSimplert () { this.$Simplert.close(); } } });
3. Clear Compilation Cache
Laravel Mix can sometimes hold onto old compiled assets. Run these commands to rebuild your resources and clear browser cache:
npm run dev # Or use watch for auto-rebuilds during development npm run watch
Then hard-refresh your browser (Ctrl+Shift+R or Cmd+Shift+R) to load the updated assets.
4. Confirm Dependency Installation
Double-check that the plugin is properly installed by running:
npm install vue2-simplert-plugin@^0.5.3 --save
This ensures the package is present in your node_modules folder with the correct version.
Once you've tried these steps, test your openSimplert method again—the <simplert> component should be recognized, and the alert should work as expected.
内容的提问来源于stack exchange,提问作者xylar

