【问题标题】:Error during instantiation of LocationStrategy in Angular2 [duplicate]在Angular2中实例化LocationStrategy时出错[重复]
【发布时间】:2016-04-15 17:28:49
【问题描述】:

我正在 angular-2 中开发一个应用程序,其中有一个索引页面,它将加载我的应用程序组件。应用组件内容路由加载主页或登录。

这是我的 index.html

<!doctype HTML>
<html>

<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >
    <script src="/angular2/bundles/angular2-polyfills.js"></script>
    <script src="/systemjs/dist/system.src.js"></script>
    <script src="/rxjs/bundles/Rx.js"></script>
    <script src="/angular2/bundles/angular2.dev.js"></script>
    <script src="/angular2/bundles/upgrade.dev.js"></script>
    <style>
       *{
            border-radius: 0px !important;
        }
    </style>
</head>

<body>
    <div class="container">
      <app>Loading......</app>
    </div>
    <script>
       System.config({
            defaultJSExtensions: true,
        packages: {        
          app: {
            defaultExtension: 'js'
          }
        },
        paths: {
            'angular2/upgrade': '../node_modules/angular2/upgrade'
          }

      });
        System.import('scripts/src/bootstrap');
    </script>
</body>

</html>

这是我的 bootstrap.ts

//import {bootstrap} from 'angular2/angular2';
///<reference path="../node_modules/angular2/typings/node/node.d.ts" />
import {UpgradeAdapter} from 'angular2/upgrade';
import {bootstrap} from 'angular2/platform/browser';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {HTTP_PROVIDERS} from 'angular2/http';


import {App} from './components/app/app';
import {LoginService} from './services/loginService';
import {HttpService} from './services/httpServices';

bootstrap(App, [HTTP_PROVIDERS, ROUTER_PROVIDERS, LoginService, HttpService]);

这是我的 app.ts

import {Component, View, Inject} from 'angular2/core';
import {Router, RouteConfig, RouterLink, RouterOutlet,  ROUTER_PROVIDERS} from 'angular2/router';


import {HomeComponent} from '../home/home';
import {LoginComponent} from '../login/login';

@Component({
    selector: 'app',
})
@View({
    templateUrl: '/scripts/src/components/app/app.html',
    directives: [RouterLink, RouterOutlet]
})
export class App {
    constructor( @Inject(Router) router: Router) {
        router.config([
            { path: '', as: 'home', component: HomeComponent },
            { path: '/login', as: 'login', component: LoginComponent }
        ]);
    }
}

它加载了我的其他组件,但是当我运行我的应用程序时出现错误。

请纠正我在这里遗漏的内容。

【问题讨论】:

标签: angular angular2-routing angular2-services


【解决方案1】:

更新(2.0.0 最终版)

@NgModule({
  providers [
    RouterModule.forRoot(myRoutes), 
    { provide: APP_BASE_HREF, useValue : 'path'}
  ],
  bootstrap: [AppComponent]
]); 

原创

https://angular.io/docs/ts/latest/guide/router.html

&lt;head&gt; 标记之后添加基本元素。如果app 文件夹是应用程序根目录,就像我们的应用程序一样,请设置href准确,如下所示。

<base href="/">

或者添加

bootstrap(AppComponent, [
  ROUTER_PROVIDERS, 
  provide(APP_BASE_HREF, {useValue : 'path'})
]); 

在您的引导程序中。

【讨论】:

  • 你为什么需要这个
  • 这被路由器和Http模块用来计算完整的URL。它配置 URL 的修复部分是什么。我自己也不知道全部细节。
猜你喜欢
  • 1970-01-01
  • 2017-04-21
  • 2014-12-04
  • 2017-11-05
  • 2017-05-27
  • 1970-01-01
  • 2019-07-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多