溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Vue export import 導(dǎo)入導(dǎo)出的多種方式與區(qū)別介紹

發(fā)布時(shí)間:2020-10-02 14:31:16 來源:腳本之家 閱讀:331 作者:呂布的僚機(jī) 欄目:web開發(fā)

在使用vue導(dǎo)出時(shí)會(huì)有一個(gè)default關(guān)鍵字,下面舉例說明下在導(dǎo)出時(shí)使用export和export default的對(duì)應(yīng)的imort寫法的區(qū)別

一、部分導(dǎo)出和部分導(dǎo)入

部分導(dǎo)出和部分導(dǎo)入的優(yōu)勢(shì),當(dāng)資源比較大時(shí)建使用部分導(dǎo)出,這樣一來使用者可以使用部分導(dǎo)入來減少資源體積,比如element-ui官方的就推薦使用部分導(dǎo)入來減少項(xiàng)目體積,因?yàn)閑lement-ui是一個(gè)十分龐大的框架,如果我們只用到其中的一部分組件, 那么只將用到的組件導(dǎo)入就可以了。

1.1部分導(dǎo)出的寫法

export function helloWorld(){
 conselo.log("Hello World");
}
export function test(){
 conselo.log("this's test function");
}

另一種寫法,這種方法比較不推薦,因?yàn)榭雌饋頃?huì)比較亂。

var helloWorld=function(){
 conselo.log("Hello World");
}
var test=function(){
 conselo.log("this's test function");
}
export helloWorld
export test

1.2部分導(dǎo)入

只導(dǎo)入需要的資源

import {helloWorld} from "./utils.js" //只導(dǎo)入utils.js中的helloWorld方法
helloWorld(); //執(zhí)行utils.js中的helloWorld方法

1.3部分導(dǎo)出——全部導(dǎo)入

如果我們需要utils.js中的全部資源則可以全部導(dǎo)入

import * as utils from "./utils.js" //導(dǎo)入全部的資源,utils為別名,在調(diào)用時(shí)使用
utils.helloWorld(); //執(zhí)行utils.js中的helloWorld方法
utils.test(); //執(zhí)行utils.js中的test方法

二、全部導(dǎo)出和全部導(dǎo)入

如果使用全部導(dǎo)出,那么使用者在導(dǎo)入時(shí)則必須全部導(dǎo)入,推薦在寫方法庫時(shí)使用部分導(dǎo)出,從而將全部導(dǎo)入或者部分導(dǎo)入的權(quán)力留給使用者。

2.1全部導(dǎo)出

需要注意的是:一個(gè)js文件中可以有多個(gè)export,但只能有一個(gè)export default

var helloWorld=function(){
 conselo.log("Hello World");
}
var test=function(){
 conselo.log("this's test function");
}
export default{
 helloWorld,
 test
}

2.2全部導(dǎo)入

import utils from "./utils.js"
utils.helloWorld();
utils.test();

總結(jié)

以上所述是小編給大家介紹的Vue export import 導(dǎo)入導(dǎo)出的多種方式與區(qū)別介紹,希望對(duì)大家有所幫助!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI