简体   繁体   English

将一个js文件包含到准备在文档中编写的另一个js文件中

[英]Include a js file to another js file written within document ready

i have a file called as sub.js 我有一个名为sub.js的文件

$(document).ready(() => console.log("sub"))

also i have a main.js file 我也有一个main.js文件

$(document).ready(() => {
   console.log("main file")
})

using es6 import can i require the sub.js file to main.js file 使用es6 import我可以将sub.js文件要求为main.js文件吗

expected output (like in the react, i hope this is the wrong expected output, but just want to know whether there is a solution) 预期输出(就像在react中一样,我希望这是错误的预期输出,但只想知道是否有解决方案)

sub.js sub.js

$(document).ready(() => console.log("sub"))
export default sub

main.js file main.js文件

import sub from './sub'
$(document).ready(() => {
   console.log("main file")
})

Change sub.js: 更改sub.js:

export function sub() { console.log("sub") }

in main.js file do: 在main.js文件中执行以下操作:

import { Sub } from './sub.js'

$(document).ready(() => {
    console.log("main file")
    sub();
})

This is using a named export . 这是使用命名的export You can also use a default export in sub.js : 您还可以在sub.js使用默认导出

export default function() { console.log("sub") }

and then 接着

import Sub from './sub.js'

$(document).ready(() => {
    console.log("main file")
    sub();
})

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

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