1,新建api.js
let api={
set(){
console.log('set')
},
get(){
console.log('get')
},
post(){
console.log('post')
}
}
export default api
-------------------------------
2,在組件中引入
importapifrom'@/api'
3,使用api
created:function(){
?? ?api.get() //打印出get
},
---------------------------------------------------------------------------------------------------------------------------------------------------export跟export default 有什么區(qū)別呢?
1、export與export default均可用于導(dǎo)出常量、函數(shù)、文件、模塊等
2、你可以在其它文件或模塊中通過import+(常量 | 函數(shù) | 文件 | 模塊)名的方式,將其導(dǎo)入,以便能夠?qū)ζ溥M(jìn)行使用
3、在一個文件或模塊中,export、import可以有多個,export default僅有一個
4、通過export方式導(dǎo)出,在導(dǎo)入時要加{ },export default則不需要
通過export方式導(dǎo)出,在導(dǎo)入時要加{ },export default則不需要。使用export default命令,為模塊指定默認(rèn)輸出