![](/img/trans.png)
[英]If a module exported as node.js export module can we import it using systemjs import method
[英]Import node module to typescript/systemjs
我正在嘗試使用react blueprint庫 ,所以我npm install
它然后我嘗試導入它像:
import { Spinner } from "@blueprintjs/core"
我得到system.src.js:1051 GET http://localhost:8888/@blueprintjs/core 404 (Not Found)
我認為它與打字有關,因為我試過的模塊上有一個ts
文件
/// <reference path="../node_modules/@blueprintjs/core/dist/index.d.ts" />
要么
/// <reference path="node_modules/@blueprintjs/core/dist/index.d.ts" />
我仍然得到同樣的錯誤。 我是Typescript的新手,如何使用這樣的節點模塊?
您需要配置SystemJS,以便它可以在瀏覽器中查找並加載所有必需的模塊。 有關一般說明,請參閱此答案 。 以下是創建藍圖微調器的完整最小示例:
安裝先決條件
npm i @blueprintjs/core
npm i react
npm i react-dom
npm i react-addons-css-transition-group
npm i @types/react
npm i @types/react-dom
npm i @types/dom4
npm i typescript
npm i systemjs
test.tsx中的示例代碼
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Spinner } from "@blueprintjs/core";
const mySpinner = <Spinner/>;
ReactDOM.render(mySpinner, document.body);
示例網頁
<!doctype html>
<html>
<head>
<link href="node_modules/@blueprintjs/core/dist/blueprint.css" rel="stylesheet" />
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script>
window.process = { env: {}};
System.config({
map: {
'react': 'node_modules/react',
'react-dom': 'node_modules/react-dom',
'react-addons-css-transition-group': 'node_modules/react-addons-css-transition-group/index.js',
'fbjs': 'node_modules/fbjs',
'tether': 'node_modules/tether/dist/js/tether.js',
'dom4': 'node_modules/dom4/build/dom4.max.js',
'@blueprintjs/core': 'node_modules/@blueprintjs/core/dist',
'classnames': 'node_modules/classnames/index.js',
'object-assign': 'node_modules/object-assign/index.js',
'pure-render-decorator': 'node_modules/pure-render-decorator/index.js'
},
packages: {
'react': { main: 'lib/React.js' },
'react-dom': { main: 'lib/ReactDOM.js' },
'fbjs': {},
'@blueprintjs/core': { main: 'index.js' },
'@blueprintjs/core/common': { main: 'index.js' },
'@blueprintjs/core/components': { main: 'index.js' }
}
});
System.import('./test.js').then(function(t) {
}).catch(function(e) {
console.error(e);
});
</script>
</head>
<body>
</body>
</html>
注意:看起來SystemJS無法使用node_modules/react/dist/react.js
和node_modules/react-dom/dist/react-dom.js
提供的bundle加載react和react-dom。 但是,如果您在瀏覽器中定義了process
變量,它可以加載來自node_modules/react/lib
和node_modules/react-dom/lib
各個源文件中的所有內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.