首页 文章

Angular 2页面路由错误[重复]

提问于
浏览
1

这个问题在这里已有答案:

Question

为什么我不能在我的路由页面上刷新而不会出错?

Problem

我的 <navigation> 指令中有 onSelect 并尝试将 PageComponent 加载到 <router-outlet> 中 . 这实际上有效,您可以看到单击测试导航链接时我的页面已加载 .

在我的真实代码中,如果我点击它,则网址会更改为 http://localhost:3000/page/10 (10只是一些ID,不相关) . 如果我现在刷新页面,我会收到错误 .

它似乎在错误的路径上搜索.js文件 /page/node_modules 而不是 /node_modules/

Errors

GET http:// localhost:3000 / page / style.css GET http:// localhost:3000 / page / vendor.js GET http:// localhost:3000 / page / node_modules / es6-shim / es6-shim . min.js GET http:// localhost:3000 / page / node_modules / systemjs / dist / system-polyfills.js GET http:// localhost:3000 / page / node_modules / angular2 / bundles / angular2-polyfills.js GET http: //localhost:3000/page/node_modules/systemjs/dist/system.src.js GET http:// localhost:3000 / page / node_modules / angular2 / bundles / angular2.dev.js GET http:// localhost:3000 / page / node_modules / rxjs / bundles / Rx.js GET http:// localhost:3000 / page / node_modules / angular2 / bundles / http.dev.js GET http:// localhost:3000 / page / node_modules / angular2 / bundles / router.dev.js未捕获的ReferenceError:未定义系统

Setup

我试图在plunkr上设置我的例子 . 但我想你真的不能刷新那里完全测试我的错误 .

https://embed.plnkr.co/7P21IbHr8EOuUW7NjmFS/

1 回答

  • 1

    可以用这个答案解决它:

    https://stackoverflow.com/a/33573548/753628

    错误是存在的,因为路线实际上不存在 . 因此我们需要在网址中使用# .

    import {provide} from 'angular2/angular2';
    import {
      ROUTER_PROVIDERS,
      LocationStrategy,
      HashLocationStrategy
    } from 'angular2/router';
    

    和引导:

    bootstrap(AppCmp, [
      ROUTER_PROVIDERS,
      provide(LocationStrategy, {useClass: HashLocationStrategy})
    ]);
    

    这会将我的URL更改为http://localhost:3000/#/page/10,如果刷新则实际上不会失败 .

相关问题