首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用类型记录1.7从角2模块导入类的麻烦

用类型记录1.7从角2模块导入类的麻烦
EN

Stack Overflow用户
提问于 2015-12-16 22:20:35
回答 5查看 15.8K关注 0票数 18

我很难理解如何从TypeScript中的模块中导入类,特别是Visual 2015中的角2类( TypeScript 1.7更新1)。

在角2文档中的任何地方,我都看到导入语句,比如:import {Component} from 'angular2/core';。这些文件在node_modules/angular2/*中。是什么让angular2/*工作呢?

只有当我有来自应用程序目录(如:./../node_modules/angular2/platform/browser'; )的相对路径时,我才能消除Visual中的错误。这修复了Visual构建错误,但当我尝试使用System.import('app/boot')运行该应用程序时,会得到如下所示的大量错误:

system.src.js:1049 GET modules/angular2/platform/browser 404 (未找到)

另一个问题是能够在Visual中使用诸如:import {SearchComponent} from './Components/Search/search.component';之类的语句,但是当我运行它时,system.src.js:2476上会出现很多GET错误。

我认为将defaultExtension: 'js'设置为System.config应该已经解决了这个问题。

UPDATE这里是我认为相关的所有文件:

views/home/index.html

代码语言:javascript
运行
复制
<script src="node_modules/es6-shim/es6-shim.js"></script>
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
System.config({
        packages: {
            app: {
                format: 'register',
                defaultExtension: 'js'
            }
        }
    });
System.import('app/app')
    .then(null, console.error.bind(console));

test.csproj

代码语言:javascript
运行
复制
<TypeScriptToolsVersion>1.7</TypeScriptToolsVersion>
<TypeScriptExperimentalDecorators>True</TypeScriptExperimentalDecorators>
<TypeScriptModuleResolution>Node</TypeScriptModuleResolution>
<TypeScriptTarget>ES5</TypeScriptTarget>
<TypeScriptJSXEmit>None</TypeScriptJSXEmit>
<TypeScriptCompileOnSaveEnabled>True</TypeScriptCompileOnSaveEnabled>
<TypeScriptNoImplicitAny>False</TypeScriptNoImplicitAny>
<TypeScriptModuleKind>CommonJS</TypeScriptModuleKind>
<TypeScriptRemoveComments>False</TypeScriptRemoveComments>
<TypeScriptOutFile />
<TypeScriptOutDir />
<TypeScriptGeneratesDeclarations>False</TypeScriptGeneratesDeclarations>
<TypeScriptNoEmitOnError>True</TypeScriptNoEmitOnError>
<TypeScriptSourceMap>True</TypeScriptSourceMap>
<TypeScriptMapRoot />
<TypeScriptSourceRoot />

typings/tsd.d.ts

代码语言:javascript
运行
复制
export * from './../node_modules/angular2/core';
export * from './../node_modules/angular2/common';
export * from './../node_modules/angular2/http';
export * from './../node_modules/angular2/router';
export * from './../node_modules/angular2/platform/browser';

文件结构:

代码语言:javascript
运行
复制
app/
    app.ts
    components/
    models/
    services/
node_modules/ (npm install using Angular 2 quickstart's package.json)
    angular2/ (not all the files listed)
        bundles/ (not all the files listed)
            angular2.dev.js
        platform/
        src/
        ts/
        typings/
        common.d.ts
        core.d.ts
        http.d.ts
        router.d.ts
    es6-module-loader/
    es6-promise/
    es6-shim/
    rxjs/
    systemjs/
    zone.js/
    typescript/ (not sure if this needs to be here)

我不熟悉TypeScript,不同的类型记录模块系统会导致错误吗?Range2建议用System.config设置format: 'register',但是https://www.npmjs.com/package/angular2说可以使用CommonJs来使用文件。

对于这些文件,我得到以下两个控制台错误:

代码语言:javascript
运行
复制
app.ts:1 Uncaught ReferenceError: require is not defined(anonymous function) @ app.ts:1
angular2-polyfills.js:143 Uncaught TypeError: Cannot read property 'split' of undefined
EN

Stack Overflow用户

发布于 2016-01-15 04:03:05

红色的小线条可能是因为你的tsconfig.json。至少这就是我遇到问题的原因。我的问题是我同时使用了files[]和excludes[]。这不像预期的那样工作,也不是一个有效的配置。

我解决了这个问题,但我仍然存在“未定义的ReferenceError: require是未定义的”问题。

票数 0
EN
查看全部 5 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34323219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档