angular.js - 使用ES3寫angular2,注入ngRouter模組報錯,該怎麼解決?
ringa_lee
ringa_lee 2017-05-15 17:07:53
0
1
732

在github上面找了一個ES5寫的angular2demo(angular2-es5-website-routes),這個demo在本地是可以運行的,但我升級了angular2相關版本到2.1後,注入angular2自帶的router模組時報錯。

zone.js:158 Uncaught Error: Component class0 is not part of any NgModule or the module has not been imported into your module.

我的程式碼如下:

boot.js

(function(app) {
  document.addEventListener('DOMContentLoaded', function() {
    // ng.platformBrowserDynamic.bootstrap(app.AppComponent, [ng.router.ROUTER_PROVIDERS]);
    ng.platformBrowserDynamic
      .platformBrowserDynamic()
      .bootstrapModule(app.AppModule);
  });
})(window.app || (window.app = {}));

app.component.js

(function(app) {
  app.AppComponent =
    ng.core.Component({
      selector: 'app',
      templateUrl: 'app/app.component.html',
      styleUrls: ['app/app.component.css'],
      // directives: [ ng.router.ROUTER_DIRECTIVES ],
      providers: [ app.StateService, app.ExperimentsService ]
    })
    .Class({
      constructor: function() {}
    });

  app.routing = [
    {path: '/',            component: app.HomeComponent},
    {path: '/home',        component: app.HomeComponent},
    {path: '/about',       component: app.AboutComponent},
    {path: '/experiments', component: app.ExperimentsComponent},
    {path: '/*',           component: app.HomeComponent }
  ];

  app.AppModule =
    ng.core.NgModule({
      imports: [ ng.platformBrowser.BrowserModule,  ng.router.RouterModule.forRoot(app.routing) ],
      declarations: [ app.AppComponent ],
      bootstrap: [ app.AppComponent ]
    })
    .Class({
      constructor: function() {}
    });
})(window.app || (window.app = {}));

index.html

<!DOCTYPE html>
<html>

<head>
    <base href="/">
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
    <link rel="icon" href="favicon.ico" title="favicon" charset="utf-8">
</head>

<body>
  <app>Loading...</app>

  <!-- Dependencies -->
  <!-- <script src="//unpkg.com/rxjs@5.0.0-beta.7/bundles/Rx.umd.js" charset="utf-8"></script>
  <script src="//unpkg.com/reflect-metadata@0.1.3" charset="utf-8"></script>
  <script src="//unpkg.com/zone.js@0.6.12" charset="utf-8"></script>
  <script src="//unpkg.com/@angular/core@2.0.0-rc.2/bundles/core.umd.js" charset="utf-8"></script>
  <script src="//unpkg.com/@angular/common@2.0.0-rc.2/bundles/common.umd.js" charset="utf-8"></script>
  <script src="//unpkg.com/@angular/compiler@2.0.0-rc.2/bundles/compiler.umd.js" charset="utf-8"></script>
  <script src="//unpkg.com/@angular/platform-browser@2.0.0-rc.2/bundles/platform-browser.umd.js" charset="utf-8"></script>
  <script src="//unpkg.com/@angular/platform-browser-dynamic@2.0.0-rc.2/bundles/platform-browser-dynamic.umd.js" charset="utf-8"></script>
  <script src="//unpkg.com/@angular/router@2.0.0-rc.2/bundles/router.umd.js" charset="utf-8"></script> -->
  <script src="node_modules/rxjs/bundles/Rx.js"></script>
  <!-- <script src="//unpkg.com/rxjs@5.0.0-beta.7/bundles/Rx.umd.js" charset="utf-8"></script> -->
  <script src="node_modules/reflect-metadata/Reflect.js"></script>
  <!-- <script src="//unpkg.com/reflect-metadata@0.1.3" charset="utf-8"></script> -->
  <script src="node_modules/zone.js/dist/zone.js"></script>
  <!-- <script src="//unpkg.com/zone.js@0.6.12" charset="utf-8"></script> -->
  <script src="node_modules/@angular/core/bundles/core.umd.js"></script>
  <!-- <script src="//unpkg.com/@angular/core@2.0.0-rc.2/bundles/core.umd.js" charset="utf-8"></script> -->
  <script src="node_modules/@angular/common/bundles/common.umd.js"></script>
  <!-- <script src="//unpkg.com/@angular/common@2.0.0-rc.2/bundles/common.umd.js" charset="utf-8"></script> -->
  <script src="node_modules/@angular/compiler/bundles/compiler.umd.js"></script>
  <!-- <script src="//unpkg.com/@angular/compiler@2.0.0-rc.2/bundles/compiler.umd.js" charset="utf-8"></script> -->
  <script src="node_modules/@angular/platform-browser/bundles/platform-browser.umd.js"></script>
  <!-- <script src="//unpkg.com/@angular/platform-browser@2.0.0-rc.2/bundles/platform-browser.umd.js" charset="utf-8"></script> -->
  <script src="node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js"></script>
  <!-- <script src="//unpkg.com/@angular/platform-browser-dynamic@2.0.0-rc.2/bundles/platform-browser-dynamic.umd.js" charset="utf-8"></script> -->
  <script src="node_modules/@angular/router/bundles/router.umd.js"></script>
  <!-- <script src="//unpkg.com/@angular/router@2.0.0-rc.2/bundles/router.umd.js" charset="utf-8"></script> -->

  <!-- Our Code -->
  <script src="app/common/state.service.js" charset="utf-8"></script>
  <script src="app/common/experiments.service.js" charset="utf-8"></script>
  <script src="app/home/home.component.js" charset="utf-8"></script>
  <script src="app/about/about.component.js" charset="utf-8"></script>
  <script src="app/experiments/experiment-details/experiment.detail.component.js" charset="utf-8"></script>
  <script src="app/experiments/experiments.component.js" charset="utf-8"></script>
  <script src="app/app.component.js" charset="utf-8"></script>
  <script src="app/boot.js" charset="utf-8"></script>
</body>

</html>

package.json

{
  "name": "fem-ng2-simple-app",
  "version": "0.0.1",
  "license": "SEE LICENSE IN LICENSE",
  "repository": {
    "type": "git",
    "url": "https://github.com/onehungrymind/fem-ng2-simple-app/"
  },
  "scripts": {
    "start": "lite-server",
    "test": "karma start"
  },
  "dependencies": {
    "@angular/common": "~2.1.0",
    "@angular/compiler": "~2.1.0",
    "@angular/core": "~2.1.0",
    "@angular/forms": "~2.1.0",
    "@angular/http": "~2.1.0",
    "@angular/platform-browser": "~2.1.0",
    "@angular/platform-browser-dynamic": "~2.1.0",
    "@angular/router": "~3.1.0",
    "@angular/upgrade": "~2.1.0",
    "angular-in-memory-web-api": "~0.1.5",
    "bootstrap": "^3.3.7",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.8",
    "rxjs": "5.0.0-beta.12",
    "zone.js": "^0.6.25"
  },
  "devDependencies": {
    "concurrently": "^3.0.0",
    "lite-server": "^2.2.2"
  }
}
ringa_lee
ringa_lee

ringa_lee

全部回覆(1)
大家讲道理

類似的npm 升級問題首要考慮的是各個安裝包的依賴問題,你抱錯的檔案是zone.js 就著重對著它的版本號做修改,在package.json 裡把zone.js 的版本號設置為預設的0.6.12 ,依然抱錯的話對應的給到0.6.13 或更高,具體的版本去https://github.com/angular/zo... 的tag 裡看,如果最新版本之前都測試過並且出錯,可以考慮看看官方文檔對於2.0 到2.1 的change-log: https://angular.io/docs/ts/la... 最新的句法只能自己研究英文文檔了,想深入研究的話建議看源碼倉庫的修改日誌https://github.com/angular/an...,希望有助於你解決類似問題,最後忠告,不要對你的依賴倉庫進行統一的升級,一但版本號最初寫死了就不要升級了,不然之後的bug 很難清理。專案越大依賴套件越多就越難排查。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板