如何在laravel中导入外部js库? [英] How import a external js library in laravel?
问题描述
我已根据以下内容从 GitHub Repo 下载了基本模板仪表板 gentella .
I've downloaded a base template from this GitHub Repo based on the following dashboard gentella.
我使用以下命令安装 inputmask 库:
I install the inputmask library with the following command:
npm install inputmask --save
但是阅读和阅读时,我不确定哪一步是将其集成到HTML页面中的正确步骤,我阅读了关于mix
,saas
,Compiling Assets (Laravel Mix)
的信息.
But reading and reading, I'm not sure which is the correct step to integrate the same into an HTML page, I read about mix
, saas
, Compiling Assets (Laravel Mix)
.
我尝试一下:
<script type="text/javascript" src="{{ URL::asset('js/inputmaskpath.js') }}"></script>
和这个:
@section('scripts')
{{ Html::script(mix('assets/js/inputmask.js')) }}
@endsection
最后一个问题是,如何将inputmask
导入到我的HTML页面中,以及正确的路径是什么?
The question finally is, how import inputmask
into my HTML page and what it is the correct path?
推荐答案
当您使用npm下载软件包时,它们将被下载到node_modules文件夹中,并且不会(直接)包含/加载到您的项目中.
When you use npm to download packages, they will be downloaded in node_modules folder and they are not (directly) included/loaded into your project.
您使用Laravel Mix来处理资产编译.因此,在您的情况下,您可以按以下方式处理它:
You use Laravel Mix to handle the assets compilation. So in your scenario you could handle it as the following:
首先在resources/assets/js
中创建一个名为app.js
的javascript文件.添加以下代码以能够加载下载的程序包
First of all create a javascript file in resources/assets/js
called app.js
. Add the following code to be able to load the downloaded package
require('inputmask');
然后使用Laravel mix您可以编译app.js
为您生成一个javasciript文件.将Laravel根文件夹中的webpack.mix.js
修改为以下内容
Then using Laravel mix you can compile app.js
to produce for you one javasciript file. Modify webpack.mix.js
in your Laravel root folder to the following
mix.js('./resources/assets/js/app.js', 'public/js/app.js');
现在,在您的视图中,您可以按以下方式加载javascript文件
Now in your view you could load the javascript file as the following
<script src="{!! mix('js/app.js') !!}"></script>
因此,如果是另一个已下载的软件包,则首先使用npm安装它,然后在resources/assets/js/app.js
文件中需要该软件包.然后Laravel mix将负责将所有必需的软件包组合到一个javascript文件中.
So, in case of another downloaded package, you install it with npm first, then in your resources/assets/js/app.js
file you require that package. Then Laravel mix will take care of combining all required packages into one javascript file.
我建议您阅读本文档以获得有关Laravel Mix的更多信息. 本教程也很有用.
I suggest you read this documentation for more information about Laravel Mix. This tutorial is useful also.
这篇关于如何在laravel中导入外部js库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!