【问题标题】:Angular2 ngIf not working in deployment environmentAngular2 ngIf 在部署环境中不起作用
【发布时间】:2016-02-24 20:13:01
【问题描述】:

我编写了一个非常简单的 Angular2 小应用程序,用于生成练习表。但是,当我部署应用程序时,ngIf 似乎不起作用(我的 ISP default webserver 或我自己的 Heroku/local 部署的 node-http-server)。当我在我的开发节点服务器 (npm start) 上运行完全相同的代码库时,ngIf 按预期工作。

如果有人对我如何调试有一些指导,我将非常感激,或者如果我只是做错了什么......

这是相关代码

src/template.html

Click on the top left word <span *ngIf="word">({{word}})</span><span *ngIf="!word">(&lt;click&gt;)</span>

app.ts

import {bootstrap} from 'angular2/platform/browser'
import {enableProdMode, Component} from 'angular2/core'
enableProdMode()

@Component({
  selector: 'app',
  templateUrl: 'src/template.html'
})
export class AppComponent {
  public word = '';
}

bootstrap(AppComponent)

当应用程序在本地启动时,我看到“单击左上角的单词 ()”(如 plunker link 所示),但是当我部署应用程序时,我只看到“单击左上角的单词” .我之前在部署中发现了与 ngFor 类似的问题。但是,我确实在 dev 和 deploy 中都看到了以下代码

<!--template bindings={}-->
<!--template bindings={}-->

所以模板正在被处理,至少在某种程度上是这样。我最好的猜测是,当我通过 gulp 生成 bundle.js 或 dependencies.js 文件s 时,一定有问题。我在开发或通过 Chrome 开发控制台部署站点上没有看到任何 Javascript 错误。

以下是我的 gulpfile 中的一些相关任务。

gulp.task('webpack', ['clean'], function() {
  return gulp.src('src/app/app.ts')
    .pipe(webpack(require('./webpack.config.js')))
    .pipe(gulp.dest('src/'));
});

罢工>

gulp.task('dependencies', function() {
  return gulp.src(['node_modules/reflect-metadata/Reflect.js', 
                   'node_modules/zone.js/dist/zone.js'])
    .pipe(concat('dependencies.js'))
    .pipe(uglify())
    .pipe(gulp.dest('dist/'));
});

我正在使用 Angular2.0.0-beta.7。我的 gulp 部署管道也使用了 Webpack1.12.2,配置如下:

webpack.config.js

module.exports = {
  entry: './src/app/app',
  output: {
    path: __dirname + '/src', publicPath: 'src/', filename: 'bundle.js'
  },
  resolve: {
    extensions: ['', '.js', '.ts']
  },
  module: {
    loaders: [{
      test: /\.ts/, loaders: ['ts-loader'], exclude: /node_modules/
    }]
  }
};

【问题讨论】:

  • 很难说。但是为什么要捆绑 Reflect 和 zone? angular2-polyfills 包已经包含它们,你正在重做它。
  • 很高兴你接受了@EricMartinez,这只是我的疏忽。当然这无助于解决问题,但它最终会导致应用程序更清洁;-)
  • 你的heroku页面有链接吗?来看看
  • @EricMartinez 这是应用程序的链接 - sudo-words.herokuapp.com。相关代码在开头的蓝色模态中。
  • 说实话很难说,真的没什么可看的。我看到您在 heroku 页面中没有使用相同的版本(您引用了在 beta.3 中添加的 trackBy)。你试过不缩小吗?您是否尝试过在您的 heroku 页面中使用 beta.0?

标签: gulp angular webpack angular2-template angular2-directives


【解决方案1】:

感谢@EricMartinez 的调试建议,该应用程序现在可以运行了。问题在于丑陋的 javascript 代码的修改。 (我将尝试追溯在处理连接的 JavaScript 文件过程中实际出了什么问题。)现在,在 uglify() 中关闭修饰就可以了,但当然我不再混淆代码了(这对我的特定实现来说不是问题)。这是修复:

gulpfile.js

gulp.task('js', ['webpack'], function() {
  return gulp.src('src/bundle.js')
    .pipe(uglify({ mangle: false })) // <- added mangle option set to false
    .pipe(gulp.dest('dist/'));
});

原来的代码是这样的

gulp.task('js', ['webpack'], function() {
  return gulp.src('src/bundle.js')
    .pipe(uglify())
    .pipe(gulp.dest('dist/'));
});

请参阅 github 上的 this issue 了解更多信息。

【讨论】:

    猜你喜欢
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 2017-06-21
    • 1970-01-01
    相关资源
    最近更新 更多