簡體   English   中英

如何在 js 中使用外部 js 文件?

[英]How do I use external js files within js?

我想縮短我的 js 中的行數。 我在文件頂部有我的 json。 我想將它存儲在一個單獨的文件中。 在 php 中,你只會做一個包含語句,但對於 js 有沒有類似的東西?

您可以通過多種方式在 js 文件之間進行通信。 es5

//FOR EXPORT use module.exports

eg. module.exports= any content(function, object, array).

//FOR IMPORT use require method

eg. const xyz = require('.path_to_your_file) //.js extension is optional

//now exported content will be available in xyz.

es6 中我們命名了 exportdefault export

// FOR **default export** use export default 
// eg. export default (any content [array,object,function])
// NOTE:- you can have only one default export in a file

// FOR named export use export 
// eg. export (any content [array,object,function])
// NOTE:- you can have multiple export in a file

///####################################
// FOR importing **default exported** content use following syntax
// import ABC from 'source file';

 //now exported content will be available in ABC.

// FOR importing **named exported** content use following syntax
// import {exported_name} from 'source file'; // see object destruct in es6 for detail
// as we can have multiple named export we can import multiple content using comma separated syntax and using.
// import { export1,export2} from 'source file';

您也可以將所有命名的導出組合在單個導入名稱中,如下所示。

//import * as ABC from 'source file';

這里所有命名導出都將在 ABC 對象中可用,您可以通過點或括號表示法訪問。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM