你能做的就是使用安装jQuery
npm install jquery
。在资源文件夹中,您可以创建一个JavaScript文件,在其中导入jQuery。
那么你应该更新你的
vite.config.js
file,将JavaScript文件添加到配置的输入中。如果您已将文件添加到列表中,则可以使用
@vite('resources/js/file-specific.js')
。现在,您只在该特定页面上包含了jQuery。
resources/js/page-specific.js
import $ from 'jquery';
// Example usage of jQuery
$(document).ready(function () {
console.log('jQuery is loaded and ready on this page!');
});
Vite配置
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
input: [
'resources/js/app.js',
'resources/js/page-specific.js', // your page-specific entry point
],
refresh: true,
}),
],
});
您的刀片文件:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Your head content -->
@vite('resources/js/page-specific.js') <!-- Include the specific JS file -->
</head>
<body>
<!-- Your body content -->
<h1>This page has jQuery loaded!</h1>
</body>
</html>