Angular2 TypeScript - 错误 TS2307:找不到模块'angular2/core'

3

我的TypeScript版本为1.7.5

我的主要组件文件

// import {Component} from 'angular2/core';
import {Component, View} from 'angular2/core';
// import {bootstrap} from 'angular2/platform/browser';

    @Component({
        selector: 'my-app',
        // template: '<h1>My title: {{title}}</h1> <h2>Hardcoded h2</h2>'
    })
    @View({
        templateUrl: '/templates/home.html',
        directives: []
    })
    .Class({
        constructor: function() {

        }
    })

export class AppComponent {
    title = "My First Angular 2 App";
}

我的HTML脚本

<!-- 1. Load libraries -->
<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>

<script src='app.component.js'></script>
<script src='app/boot.js'></script>

接下来,当我尝试编译文件 tsc app.component.ts --module system --experimentalDecorators 时,

我会得到以下错误: app.component.ts(2,31): error TS2307: Cannot find module 'angular2/core'.

你知道我错过了什么吗?


尝试将模板或templateUrl添加到@Component注释中。 - Langley
请删除.Class({constructor:function(){}})的内容,这不是一个TypeScript示例吧?你的类是:export class AppComponent {title =“我的第一个Angular 2应用程序”} - Langley
好的,我删掉了“@View”并把“templateUrl”移到了“@Component”,但是编译时仍然提示“找不到模块'angular2/core'”。另外,.Class({constructor: function()不是我可以添加逻辑的地方吗?你有类似基本结构的示例代码吗? - Leon Gaban
就像我说的那样,你的类是 export class AppComponent,这是你添加逻辑的地方,你可以在其中设置构造函数。 - Langley
请查看第二个示例@https://angular.io/ - Langley
好的,我找到了第二个例子,并且我刚刚替换了代码,现在它可以工作了。他们的System.config与我的不同,而且我认为我需要使用Google团队正在使用的最新脚本,这里有一个链接http://plnkr.co/edit/DNmmOxUzBOdbsD4eTZER?p=preview。你想发布答案吗? - Leon Gaban
1个回答

2

删除:

.Class({
    constructor: function() {

    }
})

移除:

@View({
    templateUrl: '/templates/home.html',
    directives: []
})

在@Component注释中加入templateUrl: '/templates/home.html'

应用system.js配置。 一般来说,请按照此处的步骤操作:

https://angular.io/

查看第二个示例。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接