Laravel 5.4在哪里写javascript代码?

时间:2017-02-20 16:01:18

标签: javascript laravel laravel-5.4

我真的很抱歉一个绝对愚蠢的问题,但我不明白我应该在Laravel 5.4中编写js代码。 我的网站将由两部分组成,一部分针对用户pdo_sqlite,另一部分针对管理员getProductsOfCategory(catId){ this.productsOfCategory = this.af.database.list('/categories/'+catId+'/prods'); return this.productsOfCategory .map(products => { console.log(products); // works fine: an object containing the relevant product information is logged to the console (see screenshot) products.map( product => { console.log(product.$key); // works fine: the IDs of the products are logged to the console product.prod_details = this.af.database.object('/products/'+product.$key); // seems not to work. Returned value: undefined }); }); ,每个部分都有自己的js和css文件,因此它是{{1} }

app

文件
dashboard

webpack.mix.js

mix.js('resources/assets/js/app.js', 'public/js/app.js') .js('resources/assets/js/dashboard.js', 'public/js/dashboard.js') .sass('resources/assets/sass/app.scss', 'public/css/dashboard.css') .less('resources/assets/less/app.less', 'public/css/app.css');

resources/assets/js/dashboard.js

我在哪里编写自己的代码?

2 个答案:

答案 0 :(得分:1)

app.js文件夹中的dashboard.jsresources/assets/js/文件是普通的JS文件。所以你可以直接在那里添加代码,例如:

<强> dashboard.js

require('./bootstrap');
Vue.component('example', require('./components/Example.vue'));

const app = new Vue({
    el: '#app'
});

$(document).ready(function() {
    ///Your code here
});

但不要忘记将公共文件夹中的js文件包含在HTML文件中。

如果您不想在项目中使用VueJS,可以将其完全删除。

<强> dashboard.js

require('./bootstrap');

$(document).ready(function() {
    ///Your code here
});

答案 1 :(得分:-2)

您应该创建2个文件,例如: admin.jsfront.js。然后在public/js目录中将其编译为单独的js文件:

mix.webpack('resources/assets/js/admin.js', 'public/js/admin.js')
.webpack('resources/assets/js/front.js', 'public/js/front.js')

并在布局中单独使用。

admin.js文件示例中:

window.jQuery = window.$ = require('./jquery-3.1.1.js');
require('./bootstrap');
require('./bootstrap-tagsinput');
require('./bootstrap-progressbar');
require('./custom.js');
require('./jquery.nestable.js');
require('./canvas-to-blob');
require('./purify');
require('./bordercoloranimate');
window.sweetAlert = require('./sweetalert.min.js');