2017-03-12 56 views
6

我正在構建Angular 1 + 2混合應用程序的POC。我設法運行它,現在我試圖介紹Webpack。在混合應用程序中找不到AngularJS

引導模塊時,出現Error: AngularJS v1.x is not loaded!錯誤。我已經檢查過 - 初始化模塊時加載角度。

任何幫助,將不勝感激。

項文件:

import { downgradeInjectable, downgradeComponent } from '@angular/upgrade/static'; 
import { BrowserModule } from '@angular/platform-browser'; 
import {NgModule} from '@angular/core'; 
import {HelloAngular2} from './components/hello-angular2'; 
import {TimeZonesService} from './services/timezones'; 
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 
import { UpgradeModule } from '@angular/upgrade/static'; 
import * as angular from 'angular'; 

angular.module('scotchTodo', [/*'todoController', 'todoService'*/]); 
angular.module('scotchTodo').directive('helloAngular2', downgradeComponent({ component: HelloAngular2})); 
angular.module('scotchTodo').factory('timeZones', downgradeInjectable(TimeZonesService)); 

@NgModule({ 
declarations: [HelloAngular2], 
entryComponents: [HelloAngular2], 
imports: [BrowserModule, UpgradeModule], 
providers: [TimeZonesService] 
}) 
class MyNg2Module { 
    ngDoBootstrap() {}; 
} 

platformBrowserDynamic().bootstrapModule(MyNg2Module).then(platformRef => { 
    const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule; 
    upgrade.bootstrap(document.documentElement, ['scotchTodo']); 
}); 

的WebPack配置:

var webpack = require('webpack'), 
    path = require('path'), 
    ExtractTextPlugin = require('extract-text-webpack-plugin'); 

var config = { 
    entry: { 
    app: __dirname + '/public/js/boot.js' 
    }, 
    output: { 
    path: __dirname + '/public/dist', 
    filename: 'app-loader.js' 
    }, 
    resolve:{ 
     extensions: ['.js'], 
     alias: { 
      'controllers/main.js': 'controllers/main.js', 
      'services/todos.js':'services/todos.js' 
     } 
    } 
}; 

module.exports = config; 

tsconfig.json:

{ 
    "compilerOptions": { 
    "target": "es5", 
    "module": "commonjs", 
    "moduleResolution": "node", 
    "sourceMap": true, 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "removeComments": false, 
    "noImplicitAny": false 
    }, 
    "exclude": [ 
    "node_modules", 
    "public/jslibs", 
    "typings/main", 
    "typings/main.d.ts" 
    ] 
} 

引導生成已角定義部分:

angular.module('scotchTodo', []); 
angular.module('scotchTodo').directive('helloAngular2', static_1.downgradeComponent({ component: hello_angular2_1.HelloAngular2 })); 
angular.module('scotchTodo').factory('timeZones', static_1.downgradeInjectable(timezones_1.TimeZonesService)); 
var MyNg2Module = (function() { 
    function MyNg2Module() { 
    } 
    MyNg2Module.prototype.ngDoBootstrap = function() { }; 
    ; 
    return MyNg2Module; 
}()); 
MyNg2Module = __decorate([ 
    core_1.NgModule({ 
     declarations: [hello_angular2_1.HelloAngular2], 
     entryComponents: [hello_angular2_1.HelloAngular2], 
     imports: [platform_browser_1.BrowserModule, static_2.UpgradeModule], 
     providers: [timezones_1.TimeZonesService] 
    }) 
], MyNg2Module); 
platform_browser_dynamic_1.platformBrowserDynamic().bootstrapModule(MyNg2Module).then(function (platformRef) { 
    var upgrade = platformRef.injector.get(static_2.UpgradeModule); 
    upgrade.bootstrap(document.documentElement, ['scotchTodo']); 
}); 

--UPDATE--

與此配置添加ProvidePlugin之後:

新webpack.ProvidePlugin({ '角': 'angular' })

Error: AngularJS v1.x is not loaded!我認爲這個錯誤來自於升級模塊已經消失。 現在因爲角度被__webpack_require__

--update 2--

好像問題是AngularJS不支持CommonJS的風格module.exports進口角在angular.module不確定的。我正在嘗試使用exports-loader,但現在不起作用。該配置是:生成的文件

loaders: [ 
     { test: /[\/]angular\.js$/, loader: "exports-loader?angular" } 
    ] 

--update 3--

添加module.exports = windows.angular;自舉後沒有錯誤。但是應用程序仍然不起作用,因爲角度可用沒有resumeBootstrap函數。

--update 4--

這裏是主要的HTML頁面:

<body ng-controller="mainController"> 
<div class="container"> 

    <!-- HEADER AND TODO COUNT --> 
    <div class="jumbotron text-center"> 
     <h1>I'm a Todo-aholic <span class="label label-info">{{ todos.length }}</span></h1> 
    </div> 

    <!-- TODO LIST --> 
    <div id="todo-list" class="row"> 
     <div class="col-sm-4 col-sm-offset-4"> 



      <!-- LOOP OVER THE TODOS IN $scope.todos --> 
      <div class="checkbox" ng-repeat="todo in todos"> 
       <label> 
        <input type="checkbox" ng-click="deleteTodo(todo._id)"> {{ todo.text }} 
       </label> 
      </div> 

      <p class="text-center" ng-show="loading"> 
       <span class="fa fa-spinner fa-spin fa-3x"></span> 
      </p> 

     </div> 
    </div> 

    <!-- FORM TO CREATE TODOS --> 
    <div id="todo-form" class="row"> 
     <div class="col-sm-8 col-sm-offset-2 text-center"> 
      <form> 
       <div class="form-group"> 

        <!-- BIND THIS VALUE TO formData.text IN ANGULAR --> 
        <input type="text" class="form-control input-lg text-center" placeholder="I want to buy a puppy that will love me forever" ng-model="formData.text"> 
       </div> 

       <!-- createToDo() WILL CREATE NEW TODOS --> 
       <button type="submit" class="btn btn-primary btn-lg" ng-click="createTodo()">Add</button> 
      </form> 
     </div> 
    </div> 

    <div class="text-center text-muted"> 
     <p>A demo by <a href="http://scotch.io">Scotch</a>.</p> 
     <p>Read the <a href="http://scotch.io/tutorials/javascript/creating-a-single-page-todo-app-with-node-and-angular">tutorial</a>.</p> 
    </div> 

</div> 

--UPDATE--

設置window.name = "NG_DEFER_BOOTSTRAP!";有助於角度來初始化resumeBootstrap功能但應用程序仍然不起作用

+0

任何分辨率?我目前遇到同樣的問題,它看起來代碼是非常類似於我的:-) –

+0

可以嘗試和導入webpack的副作用在樣本導入僅入口點,然後將其包含在您的index.html – harishr

+0

@harishr我沒有明白你的意思,什麼是副作用導入? – Michael

回答

1

我知道你有問題混合角1 & 2.您可能在設置過程中錯過了一些東西。

我偶然發現這個博客帖子這是有幫助的:

https://medium.com/@SevenLee/configuration-tips-to-build-hybrid-angular-1-and-angular-2-project-in-real-world-230b715629dc#.r57e4561a

此外,請問你的HTML是什麼樣子?

UPDATE

角1 =>的WebPack

角2 =>的WebPack 2

關於Webpack git倉庫建議您使用角1由不同的試劑盒,這意味着你不能在該Webpack中使用Angular 1。

他們建議你塗用NG6代替:

「如果你正在尋找角1.x中,請使用NG6-starter如果您想了解的WebPack和ES6構建工具檢查ES6 - 建設 - 工具如果你正在尋找學習打字稿看到TypeStrong /學習,打字稿如果你正在尋找的東西更容易上手,然後看到angular2種子,我也保持角/ angular2種子」

乾杯

+0

謝謝!但他的文章描述瞭如何使用systemJS運行時模塊分辨率來運行它們,並且我想使用webpack來製作一個包裝 – Michael

+0

好吧,我明白了。這是因爲Angular2和Angular2的Webpack不兼容。爲此,您需要使用不同的庫。它在[webpack的github]中提到(https://github.com/AngularClass/angular2-webpack-starter)。 然後嘗試使用[NG6](https://github.com/angularclass/NG6-starter),我不認爲它具有完全相同的功能,但它應該完成這項工作。 – mlk

相關問題