如何在全局范围内引用 JavaScript 导入?

huangapple go评论68阅读模式
英文:

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 &#39;../vendor/myjquery&#39;
import DataTable from &#39;datatables.net-dt&#39;;
import &#39;../vendor/mydatatable&#39;

mydatatable.js

console.log(&quot;dataTable?&quot;)
new DataTable(&#39;#charges-table&#39;);

myjquery.js

import jQuery from &#39;jquery&#39;;
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
    &lt;anonymous&gt; http://localhost:4000/assets/app.js:15971
    &lt;anonymous&gt; 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(&#39;#charges-table&#39;);
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(&quot;/live&quot;, Socket, { params: { _csrf_token: 
csrfToken }, hooks: Hooks })

And when you need to add it to an HTML tag, do this.

&lt;table id=&quot;table_id&quot; phx-hook=&quot;DataTableHook&quot; class=&quot;display&quot;&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;Column 1&lt;/th&gt;
            &lt;th&gt;Column 2&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;Row 1 Data 1&lt;/td&gt;
            &lt;td&gt;Row 1 Data 2&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Row 2 Data 1&lt;/td&gt;
            &lt;td&gt;Row 2 Data 2&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;

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.

huangapple
  • 本文由 发表于 2023年8月9日 05:53:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定