英文:
How do I reference javascript imports globally?
问题
我正在使用Phoenix和Elixir设置一个全新的Web项目。我在使用app.js
中导入的其他JavaScript文件中遇到了一个问题,这些文件中使用了导入的JavaScript。
我已经在应用程序中安装了DataTables.net:
npm install datatables.net-dt --save
并希望在我的app.js
中使用它,像这样:
app.js
import '../vendor/myjquery'
import DataTable from 'datatables.net-dt';
import '../vendor/mydatatable'
mydatatable.js
console.log("dataTable?")
new DataTable('#charges-table');
myjquery.js
import jQuery from 'jquery';
window.jQuery = jQuery
window.$ = jQuery
问题在于这样做不起作用,并且给出了错误:
Uncaught ReferenceError: DataTable is not defined
<anonymous> http://localhost:4000/assets/app.js:15971
<anonymous> http://localhost:4000/assets/app.js:15981
棘手的部分是,如果我在使用DataTable的文件中导入DataTable,就没有问题。不过,这似乎是一个很糟糕的限制。我该如何在整个站点的其他.js
和.html
文件中全局定义DataTables的导入呢?
英文:
I'm setting up a fresh web project in Phoenix with Elixir. I'm coming across an issue with using Javascript that is imported inside of app.js
inside of other javascript files I wish to import.
I've installed DataTables.net inside of the application with:
npm install datatables.net-dt --save
and wish to use it inside of my app.js
with an import statement, like this:
app.js
import '../vendor/myjquery'
import DataTable from 'datatables.net-dt';
import '../vendor/mydatatable'
mydatatable.js
console.log("dataTable?")
new DataTable('#charges-table');
myjquery.js
import jQuery from 'jquery';
window.jQuery = jQuery
window.$ = jQuery
The problem here is that this doesn't work and gives me the error:
Uncaught ReferenceError: DataTable is not defined
<anonymous> http://localhost:4000/assets/app.js:15971
<anonymous> http://localhost:4000/assets/app.js:15981
The tricky part is that if I import the DataTable inside of the file that is using it, there's no problem. Though, this seems like a terrible imposition. How would I define the DataTables import globally to be used throughout the site in other .js
and .html
files?
答案1
得分: 1
要访问所有网站的API,你可以这样做。
let dataTable = new DataTable('#charges-table');
Window = this.dataTable;
这将在window对象中添加一个数据表。
但我认为在这种情况下,使用JSHook可能是更好的解决方案。尝试像这样做一些事情:
在你的导入语句下面,添加以下内容。
let Hooks = {}
Hooks.DataTableHook = {
mounted(){
let tableEl = this.el;
new DataTable(tableEl, {
responsive: true
});
},
updated(){
this.mounted();
}
};
在你的Phoenix socket声明中,做如下操作:
let liveSocket = new LiveSocket("/live", Socket, { params: { _csrf_token: csrfToken }, hooks: Hooks });
当你需要将它添加到HTML标签中时,做如下操作。
<table id="table_id" phx-hook="DataTableHook" class="display">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1 Data 1</td>
<td>Row 1 Data 2</td>
</tr>
<tr>
<td>Row 2 Data 1</td>
<td>Row 2 Data 2</td>
</tr>
</tbody>
</table>
其中id可以是任何你想要的值,phx-hook调用上面声明的hook的名称。
英文:
To get access to the API throw all sites, you could do this.
let dataTable = new DataTable('#charges-table');
Window = this.dataTable
This should add a data table to the window object.
But I think a JSHook should be a better solution in this case.
Try to do something like this:
Below your imports, add this.
let Hooks = {}
Hooks.DataTableHook = {
mounted(){
let tableEl = this.el;
new DataTable(tableEl, {
responsive: true
})
},
updated(){
this.mounted()
}
}
And in your Phoenix socket declaration, do this:
let liveSocket = new LiveSocket("/live", Socket, { params: { _csrf_token:
csrfToken }, hooks: Hooks })
And when you need to add it to an HTML tag, do this.
<table id="table_id" phx-hook="DataTableHook" class="display">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1 Data 1</td>
<td>Row 1 Data 2</td>
</tr>
<tr>
<td>Row 2 Data 1</td>
<td>Row 2 Data 2</td>
</tr>
</tbody>
</table>
Where the id should be anything you want, and phx-hook calls the hook's name declared above it.
答案2
得分: 0
在JavaScript中,没有全局导入这样的概念。
英文:
There are no such things as global imports in JavaScript.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论