【关于export const 和export default】
关于export const,和 export default,这两个有什么区别
- 在一个文件中export const可以有多个,但是export default只有且仅有一个
1 // 获取后台的动态路由 2 export const getRoutersFromService = fetch({ 3 url: `${BASE_PATH}/api/misc/sys/base/menu/systemMenu/platform`, 4 method: 'post' 5 }); 6 // 列表接口 7 export const getProdList = fetch({ 8 // url: `${BASE_PATH1}/product/queryProductList`, 9 url: `${BASE_PATH1}/product/queryProductInfos`, 10 method: 'post' 11 }); 12 // 保存接口 13 export const saveForm = fetch({ 14 url: `${BASE_PATH1}/product/operation`, 15 method: 'post' 16 });
需要引入使用的时候
1 import { getProdList, saveForm, editForm } from '@/api';
- 在一个文件中export default只有且仅有一个
1 const checkUtilIn = { 2 checkName: cname => { 3 const iscname = checkUtilIn.nameElse.test(cname); 4 if (cname === null || cname === '' || cname === undefined) { 5 return '请输入姓名'; 6 } 7 if (!iscname) { 8 return '请输入正确的姓名'; 9 } 10 return ''; 11 }, 12 }; 13 export default checkUtilIn
需要引入使用的时候,【导入的时候可以给这个模块任意取名字,且不需要用花括号】
1 import checkUtil from '@/validator/checkUtil'
可以在一个文件中有多个export const,最后再用export default 统一导出
1 export const str1= 'nihao' 2 export const f1 = (a,b) => { 3 return a+b 4 } 5 export default { 6 str, 7 f1 8 }
import用于引入一个JS文件:
1、如import引入的是依赖包,则不需要相对路径;
2、如import引入的是自定义的JS文件,则需要相对路径;