2016-11-26 23 views
1

我下面這個風格指南上一個新項目ES6類的構造函數:https://github.com/toddmotto/angular-styleguide注入與角1.x和的WebPack不工作

所以我創造了這樣的模塊:

import angular from 'angular'; 
import { IndicatorSelectorComponent } from './indicatorSelector.component'; 
import { IndicatorSelectorService } from './indicatorSelector.service'; 
import './indicatorSelector.css'; 

export const IndicatorSelectorModule = angular 
    .module('indicatorSelector', []) 
    .component('indicatorSelector', IndicatorSelectorComponent) 
    .service('IndicatorSelectorService',IndicatorSelectorService) 
    .name; 

基本上進口並在模塊上註冊組件和服務。

然後創建了這個基本組成部分:

import templateUrl from './indicatorSelector.html'; 

export const IndicatorSelectorComponent = { 
    templateUrl, 
    controller: class IndicatorSelectorComponent { 
    contructor($http,IndicatorSelectorService) { 
     'ngInject'; 
     this.$http = $http; 
     this.service = IndicatorSelectorService; 
    } 

    $onInit() { 
     console.log(this.$http); 
     console.log(this.service); 
    } 
    } 
} 

的問題是,我的兩個注射($ http和IndicatorSelectorService)不工作......在控制檯上登錄時,他們是不確定的。

我懷疑它是與我的WebPack過程,所以讓我在這裏發佈的代碼:

import path from 'path'; 
import HtmlWebpackPlugin from 'html-webpack-plugin'; 
import webpack from 'webpack'; 
import ngAnnotatePlugin from 'ng-annotate-webpack-plugin'; 

export default { 
    debug: true, 
    devtool: 'inline-source-map', 
    noInfo: false, 
    entry: [ 
     path.resolve(__dirname, 'src/app/app.module') 
    ], 
    target: 'web', 
    output: { 
     path: path.resolve(__dirname, 'src'), 
     publicPath: '/', 
     filename: 'bundle.js' 
    }, 
    plugins: [ 

     // Runs ng-annotate on generated bundles 
     new ngAnnotatePlugin({ 
      add: true 
     }), 

     // Inject implicit globals for jquery 

     new webpack.ProvidePlugin({ 
      $: "jquery", 
      jQuery: "jquery", 
      "window.jQuery": "jquery" 
     }), 

     // Create HTML file that includes reference to bundled JS. 
     new HtmlWebpackPlugin({ 
      template: 'src/index.html', 
      inject: true 
     }) 
    ], 
    module: { 
     loaders: [ 
      { test: /\.jsx?$/, exclude: /(node_modules|bower_components)/, loader: 'babel' }, 
      { test: /\.css$/, loader: 'style-loader!css-loader' }, 
      { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file?name=fonts/[name].[ext]" }, 
      { test: /\.(woff|woff2)$/, loader: "url?prefix=font/&limit=5000&name=fonts/[name].[ext]" }, 
      { test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/octet-stream&name=fonts/[name].[ext]" }, 
      { test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=image/svg+xml&name=fonts/[name].[ext]" }, 
      { test: /\.(jpg|jpeg|gif|png)$/, exclude: /node_modules/, loader: 'url?limit=1024&name=images/[name].[ext]' }, 
      { test: /\.html$/, exclude: path.resolve(__dirname, 'src/index.html'), loader: 'ngtemplate!html' } 
     ] 
    } 
} 

有些人能指出我,我做錯了什麼?我感謝任何幫助!

/*編輯*/

在我的包我有這樣的生成,所以我想它被注入,但仍然不能正常工作。任何線索?

var IndicatorSelectorComponent = exports.IndicatorSelectorComponent = { 
    templateUrl: _indicatorSelector2.default, 
    controller: function() { 
    function IndicatorSelectorComponent() { 
     _classCallCheck(this, IndicatorSelectorComponent); 
    } 

    _createClass(IndicatorSelectorComponent, [{ 
     key: "contructor", 
     value: ["$http", function contructor($http) { 
     "ngInject"; 

     this.$http = $http; 
     }] 
    }, { 
     key: "$onInit", 
     value: function $onInit() { 
     console.log(this.$http); 
     } 
    }]); 

    return IndicatorSelectorComponent; 
    }() 
}; 
+0

console.log(this。$ http); console.log(this.service); 它返回undefined? – vistajess

+0

是的,它的確如此。 謝謝aswer。 我會檢查你的webpack.config.js。我試圖避免使用這種方式,因爲以我使用的方式,我不應該使用$ inject明確告知每次注射。 如果解決方案沒有出現,我可能會更改爲加載程序並使用您的方式。 – rafrcruz

回答

0

我使用的是Angular 1.5 + webpack,我也遵循該風格指南。我正在使用ng-annotate給我的裝載機。

這裏是我的webpack.config.js文件。

https://gist.github.com/vistajess/7cf451471bd06dc1b636e81eaf15e7d6

樣品角服務文件。

https://gist.github.com/vistajess/d49090e538baca96d6a9c1f884a2cac6

+0

你也可以查看我的回購角es6 webpack起動器作爲你的指導:) https://github.com/vistajess/angular-es6-webpack-starter – vistajess