[英]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.