javascript - Angular 2 路由不适用于页面刷新或使用 gulp 重新加载?

标签 javascript angular routes gulp

我从过去 1 周开始学习 Angular 2,我在使用路由时发现了一个问题。在开发阶段,我通过 npm start 运行我的应用程序。但是我使用 gulp.js 进行了迁移,当我通过键入 gulp 输出开始运行时,输出非常完美,但是当我手动重新加载/引用/更改路由时,路由不起作用,在地址栏中显示无法获取消息。

谢谢。

这是我的 gulp 文件

var gulp = require('gulp'),
    webserver = require('gulp-webserver'),
    typescript = require('gulp-typescript'),
    sourcemaps = require('gulp-sourcemaps'),
    tscConfig = require('./tsconfig.json');

var appSrc = 'builds/development/',
    tsSrc = 'process/typescript/';

gulp.task('html', function() {
  gulp.src(appSrc + '**/*.html');
});

gulp.task('css', function() {
  gulp.src(appSrc + '**/*.css');
});

gulp.task('copylibs', function() {
  return gulp
    .src([
      'node_modules/es6-shim/es6-shim.min.js',
      'node_modules/systemjs/dist/system-polyfills.js',
      'node_modules/angular2/bundles/angular2-polyfills.js',
      'node_modules/systemjs/dist/system.src.js',
      'node_modules/rxjs/bundles/Rx.js',
      'node_modules/angular2/bundles/angular2.dev.js',
      'node_modules/bootstrap/dist/js/bootstrap.min.js',
      'node_modules/bootstrap/dist/css/bootstrap.min.css',
      'node_modules/angular2/bundles/router.dev.js'
    ])
    .pipe(gulp.dest(appSrc + 'js/lib/angular2'));
});

gulp.task('typescript', function () {
  return gulp
    .src(tsSrc + '**/*.ts')
    .pipe(sourcemaps.init())
    .pipe(typescript(tscConfig.compilerOptions))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest(appSrc + 'js/'));
});

gulp.task('watch', function() {
  gulp.watch(tsSrc + '**/*.ts', ['typescript']);
  gulp.watch(appSrc + 'css/*.css', ['css']);
  gulp.watch(appSrc + '**/*.html', ['html']);
});

gulp.task('webserver', function() {
  gulp.src(appSrc)
    .pipe(webserver({
      port: '8001',
      livereload: true,
      open: true
    }));
});

gulp.task('default', ['copylibs', 'typescript', 'watch', 'webserver']);

最佳答案

这实际上不是错误。它与 HTML5 URL 有关,实际上很烦人。

这是使用哈希 Urls 的替代解决方案。

Angular2- When refresh the page, url remains same but appropriate view doesn't get loaded

关于javascript - Angular 2 路由不适用于页面刷新或使用 gulp 重新加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38972306/

相关文章:

Javascript 函数在 JSFiddle 中工作,但当我将其复制到本地页面时不起作用

java - 使用 java DSL 创建的路由不会给出路由是否正确执行的任何线索。也使用 apache AHC compoenet 作为组件之一

angular - 路由器在 async/await 方法中导航立即执行

javascript - jquery/javascript 删除 HTML 标签但没有内容

javascript - 找出何时通过 Javascript 在 EPUB FXL 中查看页面

javascript - Angular 6 url 模式验证器

angular - 是否可以使用 Bootstrap 根据 Angular 屏幕尺寸设置变量值?

express - vue中的路由中间件

javascript - 在 Javascript 中复制对象而不使用 Jquery

css - 外部 CSS 文件无法与我的 Angular 元素集成