如何在Angular 2中使用外部Web组件?

如何在Angular 2中使用外部Web组件?,angular,web-component,Angular,Web Component,我正试图弄清楚如何在我的应用程序中使用其他人(我的应用程序外部)开发的Web组件。 我有一个有用的组件,我想在我的AppWithComponent中使用。 UsefullComponent(UsefullComponent.js)存储在名为“componentsLib”的目录中,而我的应用程序代码存储在名为“myApp”的目录中。 我还有我的index.html,它导入“myApp”。 整个过程是编译的(我使用TypeScript),但在运行时我得到一个错误“Get404(NotFound)”。

我正试图弄清楚如何在我的应用程序中使用其他人(我的应用程序外部)开发的Web组件。 我有一个有用的组件,我想在我的AppWithComponent中使用。 UsefullComponent(UsefullComponent.js)存储在名为“componentsLib”的目录中,而我的应用程序代码存储在名为“myApp”的目录中。 我还有我的index.html,它导入“myApp”。 整个过程是编译的(我使用TypeScript),但在运行时我得到一个错误“Get404(NotFound)”。 知道我做错了什么吗

这是密码

UsefullComponent.ts

import {bootstrap, Component, FORM_DIRECTIVES, 
   CORE_DIRECTIVES, Input} from 'angular2/angular2';

@Component({
    selector: 'usefull-component',
    providers: [],
    template: `
        <div>
            <h2>My manifesto</h2>
            <h3>{{manifesto}}</h3>
        </div>
        `,
    styleUrls: [],
    directives: [FORM_DIRECTIVES, CORE_DIRECTIVES]
})
export class UsefullComponent { 
    public manifesto: string = 'I am a very usefull component';
}
import {bootstrap, Component, FORM_DIRECTIVES, 
        CORE_DIRECTIVES, provide} from 'angular2/angular2';
import {UsefullComponent} from '../../componentsLib/UsefullComponent';

@Component({
    selector: 'my-app-with-comp',
    providers: [UsefullComponent],
    template: `
        <div>
            <h1>I use components</h1>
            <usefull-component></usefull-component>
        </div>
        `,
    styleUrls: [],
    directives: [FORM_DIRECTIVES, CORE_DIRECTIVES, UsefullComponent]
})
class AppWithComponent { 
    public niceComponent: UsefullComponent;

    constructor(inComponent: UsefullComponent) {
        this.niceComponent = inComponent;
    }
}
bootstrap(AppWithComponent);
import{bootstrap,Component,FORM_指令,
CORE_指令,输入}来自'angular2/angular2';
@组成部分({
选择器:“有用组件”,
提供者:[],
模板:`
我的宣言
{{宣言}}
`,
样式URL:[],
指令:[形式指令,核心指令]
})
导出类UsefullComponent{
公共宣言:string='我是一个非常有用的组件';
}
AppWithComponent.ts

import {bootstrap, Component, FORM_DIRECTIVES, 
   CORE_DIRECTIVES, Input} from 'angular2/angular2';

@Component({
    selector: 'usefull-component',
    providers: [],
    template: `
        <div>
            <h2>My manifesto</h2>
            <h3>{{manifesto}}</h3>
        </div>
        `,
    styleUrls: [],
    directives: [FORM_DIRECTIVES, CORE_DIRECTIVES]
})
export class UsefullComponent { 
    public manifesto: string = 'I am a very usefull component';
}
import {bootstrap, Component, FORM_DIRECTIVES, 
        CORE_DIRECTIVES, provide} from 'angular2/angular2';
import {UsefullComponent} from '../../componentsLib/UsefullComponent';

@Component({
    selector: 'my-app-with-comp',
    providers: [UsefullComponent],
    template: `
        <div>
            <h1>I use components</h1>
            <usefull-component></usefull-component>
        </div>
        `,
    styleUrls: [],
    directives: [FORM_DIRECTIVES, CORE_DIRECTIVES, UsefullComponent]
})
class AppWithComponent { 
    public niceComponent: UsefullComponent;

    constructor(inComponent: UsefullComponent) {
        this.niceComponent = inComponent;
    }
}
bootstrap(AppWithComponent);
import{bootstrap,Component,FORM_指令,
核心指令,从“angular2/angular2”中提供};
从“../../componentsLib/UsefullComponent”导入{UsefullComponent};
@组成部分({
选择器:“我的应用程序与comp”,
提供者:[UsefullComponent],
模板:`
我使用组件
`,
样式URL:[],
指令:[格式指令、核心指令、UsefullComponent]
})
类AppWithComponent{
公共组件:UsefullComponent;
构造函数(不完整:UsefullComponent){
this.niceComponent=incomonent;
}
}
引导(AppWithComponent);
index.html

<html>
<head>
<title>Components</title>
<script src="../node_modules/es6-shim/es6-shim.js"></script>
<script src="../node_modules/systemjs/dist/system.src.js"></script>
<script src="../node_modules/angular2/bundles/angular2.dev.js"></script>

<script>
System.config({
        packages: {'myApp': {defaultExtension: 'js'}}
});
      System.import('myApp/myAppWithComp');
</script>
</head>
<body>
<my-app-with-comp>loading...</my-app-with-comp>
</body>
</html>

组件
System.config({
包:{'myApp':{defaultExtension:'js'}
});
系统导入('myApp/myAppWithComp');
加载。。。

我在代码中看到的唯一错误是组件库的路径。检查是否已将UsefullComponent.js放置在/componentsLib/UsefullComponent.js

  • 据我所知,我们应该在myApp/myAppWithComp文件中添加以下两行 引导(AppWithComponent);引导(UsefullComponent)
  • 并移除引导(AppWithComponent);从文件AppWithComponent.ts
  • 并在index.html文件中添加
    标记,如下所示

    <body><div><my-app-with-comp>loading...</my-app-with-comp></div></body>
    
    正在加载。。。
    

  • 这并不能回答这个问题。若要评论或要求作者澄清,请在其帖子下方留下评论-您可以随时对自己的帖子发表评论,一旦您有足够的评论,您就可以发表评论。-我是这里的新手,StackOverflow说我没有任何评论的名声:(谢谢Rajiran。我已经检查过了,UsefullComponent.js实际上位于/componentsLib/UsefullComponent.js;我会再尝试一下,看看是否能找到正确的位置。)wrong@rajkiran一旦你有了足够的声誉,你就可以在任何帖子上发表评论。在此之前,请意识到你的回答将完全解决这个问题。