1、export default 用法
1.1、定義函數(shù)
d.js
const a = [{name:''張三"}]
const b = [{name:''李四"}]
export default {
a,
b
}
1.2、使用
a.js
import dic from './d' // 在一個(gè)文件中,只能導(dǎo)入一次
export function test() {
console.log('dic',dic,dic.a)
}
export default
命令用于指定模塊的默認(rèn)輸出。一個(gè)模塊只能有一個(gè)默認(rèn)輸出,因此 export default
命令 只能使用(導(dǎo)入)一次
2、export 用法
2.1、定義函數(shù)
d.js
export const a = [{name:''張三"}]
export const b = [{name:''李四"}]
2.1、使用
1)使用方法1:一次性導(dǎo)入所有函數(shù)
a.js
import * as dic from './d' // * 表示一次導(dǎo)入所有的函數(shù)。as 表示起別名
export function test() {
console.log('dic',dic,dic.a)
}
這種寫法 test() 輸出的 dic對(duì)象與 export default
是一樣的,但是區(qū)別是這里的dic是module對(duì)象,export default
的 dic 是正常對(duì)象 。
2)使用方法2:按需導(dǎo)入函數(shù),使用 大括號(hào)
export 方式定義的函數(shù),使用時(shí),只想導(dǎo)入需要的函數(shù)時(shí),要使用 大括號(hào) ??梢苑侄啻螌?dǎo)入。
a.js
import {a} from './d' // 使用大括號(hào),只導(dǎo)入需要的函數(shù),可以分多次導(dǎo)入
import {b} from './d'
export function test() {
console.log('dic-a',a)
}
export function test2() {
console.log('dic-b',b)
}
3、總結(jié)
-
定義時(shí):
export default
定義函數(shù)時(shí),只能出現(xiàn)一次;export function
可以出現(xiàn)多次,不限次數(shù)。 -
使用時(shí):
導(dǎo)入export default
定義函數(shù)時(shí),只能導(dǎo)入一次。
導(dǎo)入export function
定義函數(shù)時(shí),可以一次,也可以多次,不限次數(shù)。 -
大括號(hào):
第一組export default
,對(duì)應(yīng)的 import 語句不需要使用大括號(hào);
第二組export function
,對(duì)應(yīng)的 import 語句 需要使用大括號(hào)。文章來源:http://www.zghlxwxcb.cn/news/detail-856988.html -
導(dǎo)入次數(shù)
export default
命令用于指定模塊的默認(rèn)輸出。顯然,一個(gè)模塊只能有一個(gè)默認(rèn)輸出,因此 export default 命令只能使用一次,因此 import 命令后面才不用加大括號(hào)。export function
即可以一次性全部導(dǎo)入,也可以按需導(dǎo)入。如果是按需導(dǎo)入時(shí)要使用大括號(hào)。文章來源地址http://www.zghlxwxcb.cn/news/detail-856988.html
到了這里,關(guān)于vue中 export default 與 export 寫法的區(qū)別的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!