【发布时间】:2022-11-03 12:45:58
【问题描述】:
我正在尝试在我的 Laravel 9 项目中导入 jQuery,我使用 npm i jquery 安装了该项目
我收到Uncaught ReferenceError: $ is not defined
在我的app.blade.php
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ config('app.name', 'Laravel') }}</title>
<!-- Fonts -->
<link rel="dns-prefetch" href="//fonts.gstatic.com">
<!-- Scripts -->
@vite(['resources/sass/app.scss', 'resources/js/app.js'])
</head>
在我的另一个观点中,它扩展了app.blade.php
<script>
$(function(){
alert('jquery ok');
})
</script>
在resources/js/app.js
我尝试了各种各样的事情
import $ from 'jquery';
import $ from 'jquery';
window.jQuery = $;
window.$ = $;
import inject from '@rollup/plugin-inject';
export default {
plugins: [
// Add it first
inject({
$: 'jquery',
}),
// Other plugins...
],
// The rest of your configuration...
};
try {
window.$ = window.jQuery = require('jquery');
} catch (e) {}
没有任何效果。我怎样才能解决这个问题?
npm - 8.11.0 节点 - 16.16.0 laravel/框架 - 9.32.0 维特 - 3.14
【问题讨论】:
-
@Snapey 谢谢。但这并没有帮助我解决问题。
标签: jquery laravel import vite