首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未能使用angular2导入导出类

未能使用angular2导入导出类
EN

Stack Overflow用户
提问于 2015-12-03 09:56:54
回答 2查看 601关注 0票数 0

因此,我一直试图遵循egghead.io的教程。然而,我似乎无法让step2工作。

我创建了这样的TodoInput类:

代码语言:javascript
复制
import {Component, View} from 'angular2/angular2';
@Component({
    selector: 'todo-input'
})

@View({
    template: `
        <div>This is written in the todoInput export class</div>
    `   
})

export class TodoInput{}

并在helloWorld.ts中这样使用:

代码语言:javascript
复制
import{Component,View,bootstrap} from 'angular2/angular2';
import{TodoInput} from './TodoInput';

@Component({
    selector: 'hello-world'
})

@View({
    directives: [TodoInput],
    template: `
        <div>
            <h1>This is a heading!</h1>
            <todo-input/>
        </div>
    `
})

class HelloWorld{}

bootstrap(HelloWorld);

最后,在index.html中使用hello-world标记如下:

代码语言:javascript
复制
<html>
    <head>
        <title>Angular 2 Quickstart</title>
        <script src="node_modules/traceur/bin/traceur.js"></script>
        <script src="node_modules/systemjs/dist/system.js"></script>
        <script src="node_modules/angular2/bundles/angular2.min.js"></script>
    </head>
    <body>
        <hello-world></hello-world>
        <script>
            System.import('src/helloWorld.js');
        </script>
    </body>
</html>

当我尝试运行这个程序时,我会得到一个错误:"GET /src/TodoInput“(404):"Not”。我做错了什么?

我正在运行这个版本的angular2:

代码语言:javascript
复制
"angular2/angular2.d.ts": {
  "commit": "78d36dd49b6b55b9fdfe61776a12bf05c8b07777"
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-03 10:41:14

请看下面的示例

演示

您需要为.ts文件提供helloWorld文件扩展名。

代码语言:javascript
复制
System.import("src/helloWorld").catch(console.error.bind(console)); 

休息很好。看看上面给出的链接。

票数 1
EN

Stack Overflow用户

发布于 2015-12-03 11:57:52

问题在于写index.html文件中的语句的顺序。以下是我的解决办法:

代码语言:javascript
复制
<html>
    <head>
        <title>Angular 2 Quickstart</title>
        <script src="node_modules/traceur/bin/traceur.js"></script>
        <script src="node_modules/systemjs/dist/system.js"></script>
    </head>
    <body>
        <hello-world></hello-world>
        <script>
            System.config({defaultJSExtensions:true});    
        </script>
        <script src="node_modules/angular2/bundles/angular2.min.js"></script>
        <script>
            System.import('src/helloWorld').catch(console.error.bind(console));
        </script>
    </body>
</html>

我还添加了System.config({defaultJSExtensions:true})以使src/helloWorld (没有扩展)工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34062805

复制
相关文章

相似问题

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