使用 Redux,Typescript 在不同的文件夹中打字到 JS 模块。如何告诉它模块所在的位置?

卢克

我在我的应用程序中使用 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),我导入createStorecombineReducers从中导入

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用express模块限制对node.js中许多文件夹的访问

Node.js,需要文件夹中的所有模块并直接使用已加载的模块

TypeScript在同一文件夹中找不到js模块

如何使用非模块文件中的js模块

如何在TypeScript中将不同的文件编译到不同的文件夹?

如何使用xcopy将子文件夹复制到子文件夹中?

如何使用 2 个参数重定向到文件夹 CI 中不同控制器的方法?

如何在TypeScript中为节点使用无类型的JS NPM模块?

如何使用Ubuntu One客户端将不同的文件夹同步到不同的帐户

如何使用 Symfony 3 中供应商文件夹中的 css 和 js 文件?

如何使用TypeScript加载System.js模块?

如何使用Node.js获取Google Cloud Storage文件夹中的文件列表?

如何在文件夹中合并所有JS文件并在GruntJS中使用SASS?

如何使用angular js列出文件夹中的所有文件名

如何正确使用 Redux Toolkit 中的 createAsyncThunk 和 TypeScript?

如何使用 typescript 清理 redux 工具包中的数据?

如何在模块中的函数内部使用模块的位置-python

如何使用批处理文件将特定子文件夹(如果存在)复制到备份文件夹中的新文件夹?

如何在View文件夹中使用js文件

如何使用Makefile将文件夹从父文件夹复制到Dockerfile的当前目录中

Python venv 文件夹究竟是如何工作的?我的项目使用依赖项到这个 venv 文件夹中,使用不同的 Python 版本执行项目?

Powershell GUI 如何使用选定的文件夹并将其项目复制到另一个驱动器中的不同文件夹中

如何根据Angular JS中的文件夹结构使用$ stateProvider创建状态

如何使用Node.js API在DigitalOcean存储桶中创建文件夹?

如何使用Node.js从Azure存储中删除文件夹

如何从路径模块 Node Js 指向 Html 文件夹

使用Typescript 3构建到dist /文件夹时,保持src /文件夹结构

如何使用汇总功能导入JavaScript模块并将其捆绑到单个js文件中?

如何使用Typescript在Redux reducer函数中实现详尽的switch语句?如何处理Redux的内部@@ redux动作