如何使用Angular2路由

napstablook

我正在尝试编写单页应用程序,但是无法正常工作。我尝试使用许多教程,但考虑到Angular2仍处于测试阶段,它们似乎很快过时了。

看来,只要我添加对路由器指令或路由器配置或路由器提供程序的任何引用,该应用程序就会停止工作,并且在浏览器的控制台上会显示以下错误:

Error: Zone</ZoneDelegate</ZoneDelegate.prototype.invoke@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:332:20
    Zone</Zone</Zone.prototype.run@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:227:25
    scheduleResolveOrReject/<@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:576:53
    Zone</ZoneDelegate</ZoneDelegate.prototype.invokeTask@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:365:24
    Zone</Zone</Zone.prototype.runTask@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:263:29
    drainMicroTaskQueue@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:482:26
    ZoneTask/this.invoke@http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:434:22

    Evaluating http://localhost:3000/angular2/router
    Error loading http://localhost:3000/app/main.js

现在我有以下文件,这些文件不起作用:

index.html

<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>Rapid</title>

    <!-- 1. Load libraries -->
    <!-- IE required polyfills, in this exact order -->

    <link href="css/dashboard.css" rel="stylesheet">
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <script src="node_modules/es6-shim/es6-shim.min.js"></script>
    <script src="node_modules/systemjs/dist/system-polyfills.js"></script>
    <script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>   

    <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/rxjs/bundles/Rx.js"></script>
    <script src="node_modules/angular2/bundles/angular2.dev.js"></script>
    <script src="https://code.angularjs.org/2.0.0-beta.0/http.dev.js"></script>

    <!-- 2. Configure SystemJS -->
    <script>
      System.config({
        packages: {        
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        }
      });
      System.import('app/main')
            .then(null, console.error.bind(console));
    </script>
  </head>

  <!-- 3. Display the application -->
  <body>
    <app>Loading...</app>
  </body>
</html>

main.ts

import {MainApp} from './mainApp.component';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {bootstrap} from 'angular2/platform/browser';

bootstrap(MainApp, [
    ROUTER_PROVIDERS
]);

mainApp.component.ts

import {Component} from 'angular2/core';
import {ROUTER_DIRECTIVES, RouteConfig} from 'angular2/router';
import {Home} from './home.component'

@Component({
  selector: 'app',
  template: `    
    <div>Done</div>

    `,
  directives: [ROUTER_DIRECTIVES]
})

  @RouteConfig([
    { path: '/', component: Home, as: 'Home' }
  ])

export class MainApp {}

home.component.ts

import {Component} from 'angular2/core';    

@Component({
    template: '<div>Home</div>',
})

export class Home {}

如果这是一个新手问题,很抱歉,但是从昨天开始我就一直坚持这个问题,而且我不知道该怎么解决...

潘卡·帕克(Pankaj Parkar)

确保router.dev.js(包含)app/main.js正确的位置。

您在这里错过了几件事。

  1. 做加法basehref="/"标记您的网页上一样<base href="/">
  2. 应用组件模板将具有<router-outlet></router-outlet>指令
  3. 然后useAsDefault: true用于您指定的路线

请在下面更改路线

  @RouteConfig([
     { path: '/', component: Home, as: 'Home', useAsDefault: true }
  ])

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在IIS 7.5上使用Angular2路由

来自分类Dev

Angular2路由问题

来自分类Dev

如何使用应用范围的URL前缀配置Angular2路由器?

来自分类Dev

使用ActivatedRoute检索Angular2路由上的数据属性,无论路由嵌套的级别如何

来自分类Dev

Angular2路由未由requirejs加载

来自分类Dev

Angular2路由器,内容更改,如何避免路由和重建页面

来自分类Dev

Angular2路由与Express路由一起使用?

来自分类Dev

使用(2.0.0-rc.5)路由的Angular2路由

来自分类Dev

新的Angular2路由器配置

来自分类Dev

Angular2路由路径样式参数

来自分类Dev

Angular2路由组件与父级的交互

来自分类Dev

子组件的Angular2路由链接无效

来自分类Dev

akka-http + angular2路由

来自分类Dev

Angular2路由最佳实践

来自分类Dev

Angular2路由导入错误

来自分类Dev

Angular2路由器和导航

来自分类Dev

Angular2路由路径样式参数

来自分类Dev

notFound页面的angular2路由

来自分类Dev

如何将Angular2路由器连接到ngrx / store?

来自分类Dev

如何在Node.js中处理angular2路由路径?

来自分类Dev

如何在Angular2路由器中导航到子节点的根?

来自分类Dev

Angular2路由-使用LocationStrategy添加主题标签不起作用?

来自分类Dev

Angular2路由器-canActivate使用发布请求进行重定向

来自分类Dev

Angular 2路由

来自分类Dev

.otherwise或新Angular2路由器中的/ **以通过通配符路由非路由

来自分类Dev

.otherwise或新Angular2路由器中的/ **以通过通配符路由非路由

来自分类Dev

使用LoadChildren进行Angular 2路由

来自分类Dev

如何在页面刷新时使Angular 2路由与App Engine一起使用?

来自分类Dev

Angular2路由,父级子级-路由组件与视图组件

Related 相关文章

热门标签

归档