Angular 2 - 404 提供商问题
Angular 2 - 404 provider issue
我在 Angular 2 应用程序中尝试添加提供商时遇到问题。我正在尝试我在网上找到的一些解决方案,但我无法弄清楚我的应用程序发生了什么。
基本上我在将提供者添加到我的代码时遇到了问题:
- GET http://localhost:3000/dist/services/performances.service 404(未找到)
- 错误:(SystemJS) XHR 错误(404 未找到)加载 http://localhost:3000/dist/services/performances.service(…)
注意:我检查过路径没问题,可视化代码没有给我任何错误。
在下图中,您可以看到我的应用程序的输出。
应用输出
我要添加的服务:(performances.service.ts)
import {Injectable} from '@angular/core';
import {Performance} from '../common/performance';
import {PERFORMANCES} from '../data/data-performances';
@Injectable()
export class PerformancesService {
private performances : any;
contructor(){
this.performances = {};
}
getData(){
return this.performances = PERFORMANCES;
}
}
我的app.component.ts
import {Component} from '@angular/core';
import {PerformancesService} from '../services/performances.service';
@Component({
selector : 'my-app',
template:
`
<header-fund-centre></header-fund-centre>
<div class="container">
<router-outlet></router-outlet>
</div>
`,
providers: [PerformancesService]
})
export class AppComponent { }
systemjs.config.js
(function (global) {
System.config({
paths: {
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
// angular bundles
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
// Styling
'@ng-bootstrap/ng-bootstrap': 'node_modules/@ng-bootstrap/ng-bootstrap/bundles/ng-bootstrap.js',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: '../dist/main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
'angular-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
}
}
});
})(this);
知道我在添加基本提供程序时遇到这个问题做错了什么吗?谢谢!
已编辑
下面你可以看到我目前使用的所有依赖项
"dependencies": {
"@angular/common": "~2.1.1",
"@angular/compiler": "~2.1.1",
"@angular/core": "~2.1.1",
"@angular/forms": "~2.1.1",
"@angular/http": "~2.1.1",
"@angular/platform-browser": "~2.1.1",
"@angular/platform-browser-dynamic": "~2.1.1",
"@angular/router": "~3.1.1",
"@angular/upgrade": "~2.1.1",
"@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.14",
"angular-in-memory-web-api": "~0.1.13",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"reflect-metadata": "^0.1.8",
"rxjs": "5.0.0-beta.12",
"systemjs": "0.19.39",
"zone.js": "^0.6.25"
},
"devDependencies": {
"@types/core-js": "^0.9.34",
"@types/node": "^6.0.45",
"concurrently": "^3.0.0",
"lite-server": "^2.2.2",
"typescript": "^2.0.3"
}
首先我在您的文件夹中看不到 .ts
文件,请确保您的 performances.service.ts
存在于 services
文件夹中。
将地图的map
更改为以下
map: {
// our app is within the app folder
app: 'dist', //<-- changed from 'app' to 'dist
...
}
将您的 packages
选项更改为以下,说 dist
是包
packages: {
dist: {
main: 'main.js', //<-- change here, just used shorthand
defaultExtension: 'js'
},
...
}
将 PerformancesService
添加到 NgModule
的 providers
选项,以便 DI 系统在创建它的实例时识别依赖关系。
providers: [ PerformancesService ]
我在 Angular 2 应用程序中尝试添加提供商时遇到问题。我正在尝试我在网上找到的一些解决方案,但我无法弄清楚我的应用程序发生了什么。
基本上我在将提供者添加到我的代码时遇到了问题:
- GET http://localhost:3000/dist/services/performances.service 404(未找到)
- 错误:(SystemJS) XHR 错误(404 未找到)加载 http://localhost:3000/dist/services/performances.service(…)
注意:我检查过路径没问题,可视化代码没有给我任何错误。
在下图中,您可以看到我的应用程序的输出。
应用输出
我要添加的服务:(performances.service.ts)
import {Injectable} from '@angular/core';
import {Performance} from '../common/performance';
import {PERFORMANCES} from '../data/data-performances';
@Injectable()
export class PerformancesService {
private performances : any;
contructor(){
this.performances = {};
}
getData(){
return this.performances = PERFORMANCES;
}
}
我的app.component.ts
import {Component} from '@angular/core';
import {PerformancesService} from '../services/performances.service';
@Component({
selector : 'my-app',
template:
`
<header-fund-centre></header-fund-centre>
<div class="container">
<router-outlet></router-outlet>
</div>
`,
providers: [PerformancesService]
})
export class AppComponent { }
systemjs.config.js
(function (global) {
System.config({
paths: {
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
// angular bundles
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
// Styling
'@ng-bootstrap/ng-bootstrap': 'node_modules/@ng-bootstrap/ng-bootstrap/bundles/ng-bootstrap.js',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: '../dist/main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
'angular-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
}
}
});
})(this);
知道我在添加基本提供程序时遇到这个问题做错了什么吗?谢谢!
已编辑
下面你可以看到我目前使用的所有依赖项
"dependencies": {
"@angular/common": "~2.1.1",
"@angular/compiler": "~2.1.1",
"@angular/core": "~2.1.1",
"@angular/forms": "~2.1.1",
"@angular/http": "~2.1.1",
"@angular/platform-browser": "~2.1.1",
"@angular/platform-browser-dynamic": "~2.1.1",
"@angular/router": "~3.1.1",
"@angular/upgrade": "~2.1.1",
"@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.14",
"angular-in-memory-web-api": "~0.1.13",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"reflect-metadata": "^0.1.8",
"rxjs": "5.0.0-beta.12",
"systemjs": "0.19.39",
"zone.js": "^0.6.25"
},
"devDependencies": {
"@types/core-js": "^0.9.34",
"@types/node": "^6.0.45",
"concurrently": "^3.0.0",
"lite-server": "^2.2.2",
"typescript": "^2.0.3"
}
首先我在您的文件夹中看不到 .ts
文件,请确保您的 performances.service.ts
存在于 services
文件夹中。
将地图的map
更改为以下
map: {
// our app is within the app folder
app: 'dist', //<-- changed from 'app' to 'dist
...
}
将您的 packages
选项更改为以下,说 dist
是包
packages: {
dist: {
main: 'main.js', //<-- change here, just used shorthand
defaultExtension: 'js'
},
...
}
将 PerformancesService
添加到 NgModule
的 providers
选项,以便 DI 系统在创建它的实例时识别依赖关系。
providers: [ PerformancesService ]