使用webpack打包Angular2应用程序。捆绑文件不起作用。选择器“my-app”与任何元素

时间:2016-12-02 20:36:32

标签: angular webpack

我在Angular2中有一个非常简单的应用程序,我想捆绑到一个js文件中。

我有以下dir结构:

/-client
  |
  \-app
     - main.ts
     - app.module.ts
     - app.component.ts
     - app.component.spec.ts
  -webpack.config.js 
  - ....

配置文件如下所示:

var webpack = require('webpack');

module.exports = {
entry: {
'app': './app/main.ts'
  },

output: {
  filename: 'app.js'
},

resolve: {
  extensions: ['.ts', '.js']
},

module: {
  loaders: [
  {
    test: /\.ts$/,
    loaders: ['awesome-typescript-loader', 'angular2-template-loader']
  }

]
}

};

构建完成并生成app.js(1.95MB)但是当我在网页中导入文件时出现以下错误:

app.js:26914Error: The selector "my-app" did not match any elements
at DomRenderer.selectRootElement (http://localhost:3000/app.js:18516:23)
at DebugDomRenderer.selectRootElement (http://localhost:3000/app.js:37373:39)
at selectOrCreateRenderHostElement (http://localhost:3000/app.js:10870:32)
at DebugAppView.View_AppComponent_Host0.createInternal (/AppModule/AppComponent/host.ngfactory.js:13:16)
at DebugAppView.AppView.createHostView (http://localhost:3000/app.js:38774:21)
at DebugAppView.createHostView (http://localhost:3000/app.js:39034:52)
at ComponentFactory.create (http://localhost:3000/app.js:17625:25)
at ApplicationRef_.bootstrap (http://localhost:3000/app.js:16050:40)
at http://localhost:3000/app.js:15959:89
at Array.forEach (native)

zone.js:388 Unhandled Promise rejection: Error in :0:0 caused by: The selector "my-app" did not match any elements ; Zone: <root> ; Task: Promise.then ; Value: 

任何提示我可能做错了什么?

感谢

1 个答案:

答案 0 :(得分:0)

如果没有看到剩下的代码,很难说清楚。但听起来你要么没有在my-app中定义app.component.ts的选择器:

@Component({
selector: 'my-app'
})
export class AppComponent {
...

和/或不在app.module.ts中引导该组件:

@NgModule({
  declarations: [
    AppComponent,
    ...
  ],
  imports: [
      BrowserModule,
      ...
    ],
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }