标签:bar eject 其他 导入模块 babel 语句 head rom 3.1
模块就是一段代码,这段代码可以反复使用,经常单独写成一个文件,一旦加载会立即执行。
导出有 2 种方式:命名导出和默认导出,分别用关键字export
和export default
表示
export var foo;
export let foo;
export const foo;
export function myFunc() {}
export function* myGenFunc() {}
export class MyClass {}
export {}
的{}
中var foo1;
let foo2;
const foo3;
function myFunc() {}
class MyClass {}
export {foo1, foo2, foo3, myFunc, MyClass}
export default function myFunc() {}
export default function () {}
export default function* myGenFunc() {}
export default function* () {}
export default class MyClass {}
export default class {}
export default foo;
export default ‘Hello world!‘;
export default 3 * 7;
export default (function () {});
export default moduleName
关键字导出=========================
var foo1;
export default foo1;
==========================
let foo2;
export default let foo2;
==========================
export function myFunc() {};
export default myFunc;
注意:使用命名导出时,一个 js 文件可以 export 多个模块;但是使用默认导出时,一个文件只能有一个模块导出,或者将会报错。
export { MY_CONST as FOO, myFunc };
export { foo as default };
`export * from ‘src/other_module‘;`
export { foo as myFoo, bar } from ‘src/other_module‘;
export { default } from ‘src/other_module‘;
export { default as foo } from ‘src/other_module‘;
export { foo as default } from ‘src/other_module‘
导入方式 | 语法 | 描述 |
---|---|---|
默认导入 | import localName from ‘src/my_lib‘; |
- |
命名空间导入 | import * as my_lib from ‘src/my_lib‘; |
- |
命名导入 | import { name1, name2 } from ‘src/my_lib‘; |
- |
重命名导入 | import { name1 as localName1, name2 } from ‘src/my_lib‘; |
- |
空导入 | import ‘src/my_lib‘; |
仅加载模块,不导入任何内容 |
默认导入+命名空间 | import theDefault, * as my_lib from ‘src/my_lib‘; |
- |
默认导入+命名导入 | import theDefault, { name1, name2 } from ‘src/my_lib‘; |
- |
import {ModuleName}
的方式导入;使用默认导出时,使用import moduleName
的方式导入,无需加花括号。------lib.js-----
export const PI = ‘3.14‘
export let perimeter = radius => {
console.log(‘周长:‘, 2 * PI * radius)
}
-----main.js-----
import {perimeter} from ‘lib‘
perimeter(1);
-----example.js------
if(xxx){
import example from "mock"
}
【实例 1】链接信息
-----mock.js-----
export let linkInfo = [
{
name: ‘百度‘,
address: ‘baidu.com‘
},
{
name: ‘新浪‘,
address: ‘sina.com‘
},
{
name: ‘优酷‘,
address: ‘youku.com‘
}
]
-----main.js-----
import {linkInfo} from "mock"
console.log(‘链接信息:‘,JSON.stringify(linkInfo))
【实例 2】工具类
【。。。待填坑】
【实例 3】接口
-----api.js-----
export default {
addLink (link) {
let params = {
name: link.name,
address: link.address
}
return http
.post(‘/link/add‘, params)
.then(data => {
return Promise.resolve(data)
})
.catch(e => {
return Promise.reject(e)
})
},
updateLink (link) {
let params = {
id: link.id,
name: link.name,
address: link.address
}
return http
.post(‘/link/update‘, params)
.then(data => {
return Promise.resolve(data)
})
.catch(e => {
return Promise.reject(e)
})
},
getLinkList () {
let params = {
currentPage: 1,
pageSize: 20
}
return http
.post(‘/link/list‘, params)
.then(data => {
return Promise.resolve(data)
})
.catch(e => {
return Promise.reject(e)
})
},
deleteLink (link) {
let params = {
id: link.id
}
return http
.post(‘/link/delete‘, params)
.then(data => {
return Promise.resolve(data)
})
.catch(e => {
return Promise.reject(e)
})
}
}
-----main.js-----
import * as api from "./myLib";
api.addLink(myParams)
.then(res=>{console.log("well")})
.catch(e => {console.log(e)});
标签:bar eject 其他 导入模块 babel 语句 head rom 3.1
原文地址:https://www.cnblogs.com/yejingping/p/10207345.html