0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Laravel】chart.jsをLaravel-mixを利用して入れたのに"Chart is not defined"と言われた

Last updated at Posted at 2021-10-17

chart.jsをLaravel-mixを利用して導入したものの、なぜか"Chart is not defined"「Chartなんて関数ねーぞ!」と言われてハマったので、その対処法を書きました。
 *環境
 Laravel 8.64.0(7.30.4でもできました)
 NPM 6.13.7
 Node.js 13.11.0

1.loadBefore.jsを作成

 resources/jsにloadBefore.jsを作成します。中身には以下のコードを追加します。

loadBefore.js
require('chart.js/dist/chart.js');

2.webpack.mix.jsに追記

 Laravelプロジェクトの直下にあるwebpack.mix.jsに以下のコードを追加します。

webpack.mix.js
mix.js('resources/js/loadBefore.js', 'public/js').sourceMaps();

3.Viewファイルに追記

 Viewファイルに以下のコードを追加します。

blade.php
<script src="{{asset('js/app.js')}}"></script>
//↓追記
<script src="{{ asset('js/loadBefore.js') }}"></script>

4.NPMでコンパイル

 NPMでコンパイルします。

npm run dev

5.リロード

 これで直るはず!
 chart.jsを先に読み込ませないとエラーが出るんでしょうね。

参考サイト

How to install Chart.js in Laravel? ("Uncaught ReferenceError: Chart is not defined")

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?