【问题标题】:Uncaught ReferenceError: $ is not defined - Laravel 9, ViteUncaught ReferenceError: $ is not defined - Laravel 9, Vite
【发布时间】: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

【问题讨论】:

标签: jquery laravel import vite


【解决方案1】:

你在哪里导入你的 JQuery 文件? (JS 和 CSS)

你的头部需要这样的东西:(显然您必须指定这些文件的实际路径)

<link href="./jquery-files/jquery-ui.css" rel="stylesheet">
<script src="./jquery-files/external/jquery/jquery.js"></script>
<script src="./jquery-files/jquery-ui.min.js"></script>

然后你只需要使用这样的脚本标签:

<script type="text/javascript">

    $().ready(function () {
        //add all JQuery related code here!!
        alert('jquery ok'); //this will show an alert when the page is loaded
    });

</script>

【讨论】:

    【解决方案2】:

    我的问题的答案可以在这里找到ReferenceError: $ is not defined, Jquery Import with vite

    <script type="module"> //type="module" is the important part
        $(function () {
            alert('jquery ok');
        })
    </script>
    

    【讨论】:

      【解决方案3】:

      如果你想使用 javascript动态导入因为有些页面可能不需要 jQuery。

      构建工具/捆绑器,例如ViteJs,网页包,Laravel 混合和其他人支持这一点。

      在你的app.js

      async function main() {
        const { default: jQuery } = await import('jquery')
        window.jquery = window.jQuery = window.$ = jQuery;
      
        // code that uses jQuery
        console.log('jQuery v', $().jquery);
      }
      
      main();
      

      您可以将导入放在 if 语句中

      async function main() {
      
        if (document.querySelector('.useJquery')) {
          const { default: jQuery } = await import('jquery')
          window.jquery = window.jQuery = window.$ = jQuery;
      
          // code that uses jQuery
          console.log('jQuery v', $().jquery);
        }
      }
      

      在任何需要 jQuery 的页面中,您都可以添加 class="useJquery"。

      然后只有具有useJquery 类的页面才会下载 JQuery。

      【讨论】:

        猜你喜欢
        • 2019-01-22
        • 1970-01-01
        • 2018-08-27
        • 2020-10-13
        • 2018-12-30
        • 2020-04-11
        • 2019-01-04
        • 1970-01-01
        • 2015-12-24
        相关资源
        最近更新 更多