获得EXCEPTION:无法读取属性' getCookie'在webpack上使用angular-stormpath的null

时间:2016-12-13 16:28:04

标签: angular webpack-dev-server express-stormpath

我试图让这个项目:https://github.com/stormpath/stormpath-angular2-express-example在webpack上运行。应用程序启动,但是当第一页加载时我收到此错误:

TypeError: Cannot read property 'getCookie' of null
at CookieXSRFStrategy.configureRequest (app.js:51374)
at XHRBackend.createConnection (app.js:51414)
at httpRequest (app.js:51752)
at StormpathHttp.Http.get (app.js:51863)
at StormpathHttp.get (app.js:78381)
at Stormpath.getAccount (app.js:49917)
at new Stormpath (app.js:49906)
at _View_AppComponent_Host0.createInternal (host.ngfactory.js:20)
at _View_AppComponent_Host0.AppView.create (app.js:42393)
at _View_AppComponent_Host0.DebugAppView.create (app.js:42593)

我在这里设置了一个样本回购设置:https://github.com/djkrite/ng2-stormpath-webpack-example

我试图使用:

BrowserDomAdapter.makeCurrent();

在引导程序调用之前,但最终会引发有关意外令牌导入的另一个错误。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

好消息!我想出了如何使ng2-stormpath-webpack-example工作。我认为问题的根本原因是打字稿配置(tsconfig.json)位于src目录中,而webpack希望它位于根目录中。这是tsconfig.json文件有效(请记住,我将其移动到根目录):

{
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es6", "dom"],
    "mapRoot": "./",
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "../dist/out-tsc",
    "sourceMap": true,
    "target": "es5",
    "types": [
      "jasmine",
      "node"
    ],
    "exclude": [
      "node_modules"
    ]
  }
}

我将webpack.config.js中的webpack加载程序更改为:

module: {
    loaders: [
      {
        test: /\.ts$/,
        loaders: ['awesome-typescript-loader', 'angular2-template-loader?keepUrl=true'],
        exclude: [/\.(spec|e2e)\.ts$/, /node_modules/]
      },
      /* Embed files. */
      {
        test: /\.(html|css)$/,
        loader: 'raw-loader',
        exclude: /\.async\.(html|css)$/
      },
      /* Async loading. */
      {
        test: /\.async\.(html|css)$/,
        loaders: ['file?name=[name].[hash].[ext]', 'extract']
      }
    ]
  }

我还删除了空的解析扩展程序:

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

最后,在package.json中,我为Protractor添加了类型并升级到Webpack 2.x。

  "devDependencies": {
    ...
    "@types/selenium-webdriver": "2.53.33",
    ...
    "webpack": "2.2.0-rc.1",
    "webpack-dev-server": "^1.16.2"
  }

这是一个带有修复程序的拉取请求:https://github.com/djkrite/ng2-stormpath-webpack-example/pull/1