laravel 在 app.js 中包含自定义 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39778765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:32:17  来源:igfitidea点击:

Include custom JavaScript in the app.js

javascriptlaravelassets

提问by Sergio Vilchis

I want to include a custom JavaScript in the app.js file which as the following code:

我想在 app.js 文件中包含一个自定义 JavaScript,其代码如下:

window.$ = window.jQuery = require('jquery')
require('bootstrap-sass');

The require only works for published packages and using node install but I want to know if there is a way to include a custom js in this file in order to have just one built script in my app (in this case the app.js script).

require 仅适用于已发布的包并使用 node install 但我想知道是否有办法在此文件中包含自定义 js 以便在我的应用程序中只有一个内置脚本(在这种情况下为 app.js 脚本) .

回答by amrography

Add your scripts under this path \resources\assets\js\,

在此路径下添加您的脚本\resources\assets\js\

(ex : \resources\assets\js\scripts\my_script.js& \resources\assets\js\another_script.js)

(例如:\resources\assets\js\scripts\my_script.js\resources\assets\js\another_script.js

And, add them to \resources\assets\js\app.js

并且,将它们添加到 \resources\assets\js\app.js

require ('./scripts/my_script.js');
require ('./another_script.js');

回答by Camilo Rojas

Just add your script in resources/assets/jsand then run gulpto compile all your scripts into app.jsCheck this for more info

只需添加您的脚本,resources/assets/js然后运行gulp即可将所有脚本编译为app.js查看更多信息

回答by Daniel Verem

I presume you have other files that are not exactly VueJs files and you want to build into a single script. In that case it is not good to put the script in that file. You can however place the files in your resources/assets/jsfolder and add some code to your gulpfile.jsto build all your scripts into one file. Here is the code you might need to add.

我假设您有其他不完全是 VueJs 文件的文件,并且您想构建到单个脚本中。在这种情况下,将脚本放在该文件中是不好的。但是,您可以将文件放在您的resources/assets/js文件夹中并向您添加一些代码以将gulpfile.js所有脚本构建到一个文件中。这是您可能需要添加的代码。

 mix.scripts([
   'filename.js',
 ]);

By default, the build file will be placed in public/js/all.js.

默认情况下,构建文件将放置在public/js/all.js.

You can customise that driectory by modifying the example above to something like this

您可以通过将上面的示例修改为这样的内容来自定义该目录

mix.scripts([
  'filename.js
], 'path/to/file.js');