我在我的应用程序中使用 Redux。我正在使用 Visual Studio 2015 和 npm 作为依赖项下载 Redux。
我有以下文件夹结构:
MySolution.sln
├── node_modules
│ ├── redux
│ │ ├── index.d.ts
| | ├── dist
│ │ | ├── redux.js
│ │ | ├── redux.min.js
├── Scripts
│ ├── myscript.ts
│ ├── myscript.js
Redux 的节点结构与使用 npm 下载时一样
npm 安装 redux
我有一个使用 Redux 的 TypeScript 脚本 (myscript.ts),我导入createStore
并combineReducers
从中导入。
myscript.ts
/// <reference path="../node_modules/redux/index.d.ts" />
import { combineReducers, createStore } from "../node_modules/redux/index";
//... more code
const myApp = combineReducers({
SelectedAnswers: selectedAnswers
});
var store = createStore(myApp);
编译为以下(myscript.js):
define(["require", "exports", "../node_modules/redux/index"], function (require, exports, index_1) {
"use strict";
var _this = this;
Object.defineProperty(exports, "__esModule", { value: true });
var questionApp = index_1.combineReducers({
SelectedAnswers: selectedAnswers
});
var store = index_1.createStore(questionApp);
});
如您所见,我的代码使用了导入脚本,这是由 Visual Studio/TypeScript 自动生成的。
这对于编译目的来说很好,因为它指向../../../node_modules/redux/index
哪个将允许它选择index.d.ts
哪个是 TypeScript 类型文件(绝对类型)。
所以我的问题出现在应用程序实际运行时,Redux 的真正脚本在下一个文件夹中/redux.js
,所以当我运行我的脚本时,它告诉我该模块无法加载。
我如何告诉 TypeScript 脚本/redux
在运行时存在?
我认为如果我require.config
在 TypeScript 生成的define()
方法中出现的名称 htat 中指定它,我可以告诉 RequireJS 脚本在哪里,但这没有任何效果:
require.config({
baseUrl: "/",
paths: {
"../node_modules/redux/index": "../node_modules/redux/dist/redux"
}
});
谢谢你的帮助。
注意:如果我移动index.d.ts
到与它相同的文件夹redux.js
并调用它redux.d.ts
,它工作正常,但这不是它在 npm 下载中的显示方式。我不想每次更新时都将它们移动到同一个文件夹中。
好吧,看来我痛苦的主要来源是我混淆了内部模块和外部模块的概念。
外部模块通常不被它们在构成应用程序的 TypeScript 文件挂毯中的相对路径引用,这些应该在require.config()
.
可以使用相对路径引用内部模块和 TypeScript 文件。
所以答案是redux
,外部模块应该在 TypeScript 中通过 redux 定义文件中指定的名称进行引用。
import { combineReducers, createStore } = require("redux");
//... now we can use the imported external module
const myApp = combineReducers({
SelectedAnswers: selectedAnswers
});
var store = createStore(myApp);
你还必须确保你有外部模块的类型,我没有这个,所以这导致我的 TypeScript 文件编译失败并出现错误Cannot find module 'redux'
。所以我通过安装绝对类型化的定义来获得类型,这允许 TypeScript 文件编译:
安装包 react-redux.TypeScript.DefinitelyTyped
似乎这通常适用于您可能想要导入的大多数外部模块,但如果不是,您可以指定自己的定义(将其放在解决方案中的 *.d.ts 文件中),这将允许您要编译的 TypeScript 文件:
// This can be redux, or any other module that you'd like to use in require() to enable it to compile your TypeScript code
declare module "redux" {
var _temp: any;
export = _temp;
}
如果这样做,自然不会得到智能感知,因此首选 TypeScript 类型。
在设置 you 的属性时require.config()
,它被称为应用程序的入口点,您应该:
baseUrl
这是我的示例的入口点示例(main.ts,编译为 main.js 以供稍后使用):
require.config({
baseUrl: "/", // The base url
paths: {
// Named external module (redux in this case)
"redux": "../node_modules/redux/dist/redux"
}
});
// Bootstrap the entry point module, notice that this is an 'internal module', hence the relative location being used as the module name
require(["Scripts/myscript"], () => {
console.log("Your internal module has been resolved and should be running now!")
});
这可以从您的应用程序页面中调用,如下所示:
<script "/node_modules/requirejs/require.js" data-main="scripts/main.js"></script>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句