What are the functions of export and export default
This article mainly explains "what is the role of export and export default". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "what is the role of export and export default" together!
The export command specifies the external interface of the module. A module is an independent file. All variables inside this file cannot be obtained externally. If you want external access to a variable inside a module, you must export the variable using the export keyword. Below is a JS file that uses the export command to output variables.
Use of export
1. directly output
exportletwords='helloworld!!! '
exportfunctionoutput(){
//...
}
2. Define before you export
copy code
letfirstWords='hello'
letsecondWords='world'
letthirdWords='!!! '
functionoutput(){
//...
}
export{firstWords,secondWords,thirdWords,output}
copy code
Use of exportdefault
1.exportdefault Used to specify the default external interface of the module
2. Obviously, there can only be one external interface by default, so exportdefault can only appear once in the same module.
3.exportdefault can only be output directly, can not be defined first and then output.
4. There is also a difference between import and export.
(1)export output and import input
exportfunctionoutput(){
//...
}
import{output}from'./ example'
(2) export default output and import input
exportdefaultfunctionoutput(){
//...
}
importoutputfrom'./ example'
As you can see from the above two import methods, the import method of exportdefault does not need to be wrapped in braces. Because there is only one interface for the output of exportdefault, the default interface of the module is provided, and naturally there is no need to wrap it in braces.
Thank you for reading, the above is "export and export default what is the role" of the content, after the study of this article, I believe that we have a deeper understanding of the role of export and export default, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!