简体   繁体   English

Laravel livewire,如何在刀片中包含 javascript 文件

[英]Laravel livewire, How to include javascript file in blade

I will try to avoid duplicate questions and be as clear as possible.我会尽量避免重复的问题并尽可能清楚。 I recently created a js script to do a filtered search of a table and I need this file to be included on multiple blades.我最近创建了一个 js 脚本来对表进行过滤搜索,我需要将此文件包含在多个刀片上。 The problem is that the script works if it is pasted into the blade, but I cannot include it properly into the blade, I tried to put it in the public folder and install mix, unsuccessful in both cases.问题是如果将脚本粘贴到刀片中,该脚本可以工作,但我无法将其正确包含到刀片中,我试图将其放入公用文件夹并安装 mix,在这两种情况下都不成功。 I'm sure that the problem is the folder where i put the js file and how i try to include it, but i didn't find a solution even though I tried in every way.我确定问题出在我放置 js 文件的文件夹以及我如何尝试包含它,但即使我尝试了各种方式,我也没有找到解决方案。

This is my js file(filtering_script.js):这是我的 js 文件(filtering_script.js):

 //ricerca filtrata tra le tabelle

function Filtering() {
// Dichiara le variabili
var input, filter, table, tr, td, i, txtValue;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");


for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
  txtValue = td.textContent || td.innerText;
  if (txtValue.toUpperCase().indexOf(filter) > -1) {
    tr[i].style.display = "";
  } else {
    tr[i].style.display = "none";
  }
}
}
}

I know it's probably a stupid problem but i can't really solve it, hope someone helps me, thank you.我知道这可能是一个愚蠢的问题,但我无法真正解决它,希望有人帮助我,谢谢。

Apparently the question is not appreciated, but i think it might be useful to some so i will post the solution for people starting with laravel.显然这个问题不受欢迎,但我认为它可能对某些人有用,所以我会为从 laravel 开始的人发布解决方案。 If you use livewire you should remember NOT to use npm run dev, but use npm run build, once done create two folders, 'css' and 'js' inside the public folder, then put your file here so they will be accessible to the rest of the components.如果你使用 livewire,你应该记住不要使用 npm 运行开发,而是使用 npm 运行构建,完成后在公共文件夹中创建两个文件夹,“css”和“js”,然后将文件放在这里,以便他们可以访问rest 的组件。 The code to put inside the blade, for javascript, is:对于 javascript,放置在刀片内部的代码是:

<script src="{{url('js/yourfile.js')}}"> </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM