无法在客户端使用模块
问题描述:
我在尝试修复它时遇到了不同的错误。这次我试图使用一个模块来上传文件。现在,我得到这个错误:无法在客户端使用模块
"Error: Can't resolve all parameters for FileUploader: (?).
Evaluating http://localhost:3333/app/main.js
Loading app
at ZoneAwareError (http://localhost:3333/libs/zone.js/dist/zone.js:992:33)
at LoaderError__Check_error_message_for_loader_stack (http://localhost:3333/libs/systemjs/dist/system.src.js:80:11)
at http://localhost:3333/libs/systemjs/dist/system.src.js:284:11
at ZoneDelegate.invoke (http://localhost:3333/libs/zone.js/dist/zone.js:334:26)
at Zone.run (http://localhost:3333/libs/zone.js/dist/zone.js:126:43)
at http://localhost:3333/libs/zone.js/dist/zone.js:713:57
at ZoneDelegate.invokeTask (http://localhost:3333/libs/zone.js/dist/zone.js:367:31)
at Zone.runTask (http://localhost:3333/libs/zone.js/dist/zone.js:166:47)
at drainMicroTaskQueue (http://localhost:3333/libs/zone.js/dist/zone.js:546:35)
at <anonymous>"
如果我不把FileUploader的供应商,我得到另一个错误:
Error: DI Error
at NoProviderError.ZoneAwareError (http://localhost:3333/libs/zone.js/dist/zone.js:992:33)
at NoProviderError.BaseError [as constructor] (http://localhost:3333/@angular/core/bundles/core.umd.js:1239:20)
at NoProviderError.AbstractProviderError [as constructor] (http://localhost:3333/@angular/core/bundles/core.umd.js:1365:20)
at new NoProviderError (http://localhost:3333/@angular/core/bundles/core.umd.js:1405:20)
at ReflectiveInjector_._throwOrNull (http://localhost:3333/@angular/core/bundles/core.umd.js:2937:23)
at ReflectiveInjector_._getByKeyDefault (http://localhost:3333/@angular/core/bundles/core.umd.js:2976:29)
at ReflectiveInjector_._getByKey (http://localhost:3333/@angular/core/bundles/core.umd.js:2908:29)
at ReflectiveInjector_.get (http://localhost:3333/@angular/core/bundles/core.umd.js:2777:25)
at AppModuleInjector.NgModuleInjector.get (http://localhost:3333/@angular/core/bundles/core.umd.js:8491:56)
at CompiledTemplate.proxyViewClass.AppView.injectorGet (http://localhost:3333/@angular/core/bundles/core.umd.js:11935:49)
at CompiledTemplate.proxyViewClass.DebugAppView.injectorGet (http://localhost:3333/@angular/core/bundles/core.umd.js:12315:53)
at ElementInjector.get (http://localhost:3333/@angular/core/bundles/core.umd.js:11790:31)
at ReflectiveInjector_._getByKeyDefault (http://localhost:3333/@angular/core/bundles/core.umd.js:2973:28)
at ReflectiveInjector_._getByKey (http://localhost:3333/@angular/core/bundles/core.umd.js:2908:29)
at ReflectiveInjector_.get (http://localhost:3333/@angular/core/bundles/core.umd.js:2777:25)
反正我得到任何模块,我尝试在客户端上使用这种类型的错误这样我就失去了一些东西。如果它帮助我起初得到了<匿名>响应,并且我认为我解决了这个问题,但也许我刚刚尝试修复它时发生了另一个错误。
我app.module.ts:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { routing } from './app.routing';
import { UsersComponent } from './components/users/users.component';
import { UserDetailComponent } from './components/userDetail/user-detail.component';
import { UserService } from './services/user.service';
import { FileSelectDirective, FileDropDirective, FileUploadModule, FileUploader } from 'ng2-file-upload';
@NgModule({
imports: [
BrowserModule,
HttpModule,
FormsModule,
routing,
FileUploadModule
],
declarations: [
AppComponent,
UsersComponent,
UserDetailComponent
],
providers: [
UserService,
FileUploader
],
bootstrap: [AppComponent]
})
export class AppModule { }
我的组件:
import {Component, Input, OnInit} from '@angular/core';
import {User} from "../../models/user";
import { ActivatedRoute, Params } from '@angular/router';
import {UserService} from "../../services/user.service";
import { FileUploader } from 'ng2-file-upload';
@Component({
selector: 'my-user-detail',
templateUrl: './app/components/userDetail/user-detail.component.html'
})
export class UserDetailComponent implements OnInit {
@Input() user: User;
newUser = false;
error: any;
navigated = false; // true if navigated here
constructor(
public uploader:FileUploader = new FileUploader({url:'http://localhost:3333/users/file'}),
private userService: UserService,
private route: ActivatedRoute) {
}
ngOnInit() {
this.route.params.forEach((params: Params) => {
let id = params['id'];
if (id === 'new') {
this.newUser = true;
this.user = new User();
} else {
this.newUser = false;
this.userService.getUser(id)
.then(user => this.user = user);
}
});
}
save() {
this.userService
.save(this.user)
.then(user => {
this.user = user; // saved user, w/ id if new
this.goBack();
})
.catch(error => this.error = error); // TODO: Display error message
}
goBack() {
window.history.back();
}
}
systemconfig.js:
var isPublic = typeof window != "undefined";
(function(global) {
// map tells the System loader where to look for things
var map = {
'app': 'app', // 'dist',
'@angular': (isPublic)? '@angular' : 'node_modules/@angular',
'rxjs': (isPublic)? 'rxjs' : 'node_modules/rxjs',
'ng2-file-upload': (isPublic)? 'ng2-file-upload' : 'node_modules/ng2-file-upload'
};
// packages tells the System loader how to load when no filename and/or no extension
var packages = {
'app': { main: 'main.js', defaultExtension: 'js' },
'rxjs': { defaultExtension: 'js' },
'ng2-file-upload' : { main: 'ng2-file-upload.js', defaultExtension: 'js'}
};
var ngPackageNames = [
'common',
'compiler',
'core',
'forms',
'http',
'platform-browser',
'platform-browser-dynamic',
'router',
'router-deprecated',
'upgrade'
];
// Individual files (~300 requests):
function packIndex(pkgName) {
packages['@angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
}
// Bundled (~40 requests):
function packUmd(pkgName) {
packages['@angular/'+pkgName] = { main: 'bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
}
// Most environments should use UMD; some (Karma) need the individual index files
var setPackageConfig = System.packageWithIndex ? packIndex : packUmd;
// Add package entries for angular packages
ngPackageNames.forEach(setPackageConfig);
var config = {
map: map,
packages: packages
};
System.config(config);
})(this);
我怎么称呼它的index.html:
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
谢谢。
答
您不能在构造函数中注入FileUploader
。从构造FileUploader =新FileUploader`:
按照demo这应该为你工作
export class UserDetailComponent implements OnInit {
public uploader: FileUploader = new FileUploader({new FileUploader({url:'url'})
+0
我知道这是前一段时间,但你会知道为什么这总是发送空的身体请求?自从我很忙,我没有告诉任何事情,并希望自己找到解决方案。我找到另一种上传imgs的方式,但只有这样。这就是为什么我现在问这个问题:/你能帮我吗?这是相同的代码。 – Elanochecer
删除'公共上传。你看过演示了吗? http://valor-software.com/ng2-file-upload/ – yurzui
是的,我做了,但我认为它会在构造函数中确定,只是写在那里,再也没有想过它XD非常感谢你,现在我只是必须修复服务器端XD – Elanochecer