找不到模块' @ angular / platform / browser'

时间:2016-08-08 11:59:42

标签: angular angular2-routing

我使用angular2创建了我的ng new myproject应用,现在我要引导我的应用。这是我的boot.ts文件:

import {bootstrap}    from '@angular/platform/browser';
import {AppComponent} from './app.component'
import {ROUTER_PROVIDERS} from '@angular/router'

bootstrap(AppComponent,[ROUTER_PROVIDERS] );

此文件中有2个问题: 在第一行'@angular/platform/browser',它抱怨

Cannot find module '@angular/platform/browser'.

并在第三行import {ROUTER_PROVIDERS} from '@angular/router'中抱怨:

Module '"c:/Users/Salman/Desktop/Courses/Internship/Angular2/Qminder/Qminder/node_modules/@angular/router/index"' has no exported member 'ROUTER_PROVIDERS'.

那么我该怎么办呢? 命令ng new有什么问题吗?

1 个答案:

答案 0 :(得分:2)

从Angular Release Candidate版本4开始,您应该从以下模块导入您的引导函数:

import { bootstrap }  from '@angular/platform-browser-dynamic';

路由器模块在最新版本的angular中更改,因此您需要创建“app.routes.ts”文件,该文件具有以下内容:

import { provideRouter, RouterConfig } from '@angular/router';

const routes: RouterConfig = [
  { path: 'home', component: HomeComponent },
  { path: '**', component: PageNotFoundComponent }
];

export const appRouterProviders = [
  provideRouter(routes)
];

然后你需要导入“appRouterProviders”并将其提供给“bootstrap”方法:

import { bootstrap }          from '@angular/platform-browser-dynamic';
import { AppComponent }       from './app.component';
import { appRouterProviders } from './app.routes';

bootstrap(AppComponent, [
  appRouterProviders
])
.catch(err => console.error(err));

有关最新路由器的更多信息,请访问:

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