![](/img/trans.png)
[英]How are the imports implemented in different React libraries and how can I implement the same pattern in my own library?
[英]How can I use React style imports for my own files?
我想使用 React 或本例中经常使用的这种形式 react-redux
import redux, { useSelector, useDispatch } from 'react-redux';
所以我尝试了明显的:
我创建了一个导出文件:
const exp = {};
exp.a = 'a-1';
exp.b = 'b-1';
export default exp;
以及导入它的文件:
import {a, b} from './40-export.js'; // this does not work
// import test from './40-export.js'; // this works
// const {a, b} = test; // this works
您需要同时具有export default
和命名导出。 制作您导出的独立a
和b
变量,并将其分配给默认导出 object 的属性。
export const a = 'a-1';
export const b = 'b-1';
export const exp = { a, b }; // if you want this to be a named export too
export default exp;
export const a = 2
export const b = 3
export default const c = 3
然后
import c , {a, b} from "your/file"
注意{...}
用于命名导入,而默认导出则缺少它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.