Angular2:this.http.get(...).map不是一个函数,但只有在AoT编译时(Angular2: this.http.get(…).map is not a function but only when AoT compilation)
我正在使用服务来发出http请求:
import { Headers, Http } from '@angular/http'; import { Observable } from 'rxjs'; import 'rxjs/add/operator/map'; .... classcode..... getOffices(): Observable<Office[]> { const url = `${this.apiurl}office` console.log(url); return this.http.get(url) .map(response => response.json()); };
这一切都适用于我正在开发时使用的SystemJS。 但是,当我使用汇总和uglify进行AoT编译时,我不断收到错误:
bundle.min.js:7 EXCEPTION:Uncaught(在promise中):TypeError:this.http.get(...)。map不是函数
我可以找到的所有人都说要将
import 'rxjs/add/operator/map'
到main.ts文件或app.module.ts文件中。 我已将此添加到每个相关文件中,并将import 'rxjs/Rx'
到所有相关文件,但似乎没有任何工作..有没有人成功地将http请求与AoT编译结合使用?
这是我用于AoT的de tsconfig文件:
{ "compilerOptions": { "target": "es2015", "module": "es2015", "moduleResolution": "node", "declaration": false, "removeComments": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "sourceMap": true, "pretty": true, "allowUnreachableCode": false, "allowUnusedLabels": false, "noImplicitAny": true, "noImplicitReturns": true, "noImplicitUseStrict": false, "noFallthroughCasesInSwitch": true, "outDir": "./dist", "rootDir": "./compiled" }, "compileOnSave": false, "files": [ "compiled/main-ngc.ts" ], "filesGlob":[ "typings.d.ts", "compiled/main-ngc.ts", "compiled/main.ts" ], "exclude": [ "node_modules" ] }
这些是我正在使用的构建命令:
"build": "tsc -p tsconfig-compiled.json && gulp libs", "rollup": "rollup -f iife -c -o dist/bundle.es2015.js", "es5": "tsc --target es5 --allowJs dist/bundle.es2015.js --out dist/bundle.js", "build_prod": "npm run ngc && npm run build && npm run rollup && npm run es5 && npm run minify && gulp clean:build && gulp build_prod", "minify": "uglifyjs dist/bundle.js --screw-ie8 --compress --mangle --output dist/bundle.min.js",
任何帮助将受到欢迎!
I'm using a service to make http get requests:
import { Headers, Http } from '@angular/http'; import { Observable } from 'rxjs'; import 'rxjs/add/operator/map'; .... classcode..... getOffices(): Observable<Office[]> { const url = `${this.apiurl}office` console.log(url); return this.http.get(url) .map(response => response.json()); };
This all works when using SystemJS, which I'm using while I'm developing. However, when I use AoT compilation with rollup and uglify I keep getting the error:
bundle.min.js:7 EXCEPTION: Uncaught (in promise): TypeError: this.http.get(...).map is not a function
All I can find is people saying to add
import 'rxjs/add/operator/map'
to the main.ts file or the app.module.ts file. I've added this to every related file as well as addedimport 'rxjs/Rx'
to all related file but nothing seems to work..Has anybody been successful in using http request in combination with AoT compilation?
this is de tsconfig file I'm using for AoT:
{ "compilerOptions": { "target": "es2015", "module": "es2015", "moduleResolution": "node", "declaration": false, "removeComments": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "sourceMap": true, "pretty": true, "allowUnreachableCode": false, "allowUnusedLabels": false, "noImplicitAny": true, "noImplicitReturns": true, "noImplicitUseStrict": false, "noFallthroughCasesInSwitch": true, "outDir": "./dist", "rootDir": "./compiled" }, "compileOnSave": false, "files": [ "compiled/main-ngc.ts" ], "filesGlob":[ "typings.d.ts", "compiled/main-ngc.ts", "compiled/main.ts" ], "exclude": [ "node_modules" ] }
and these are the build commands I'm using:
"build": "tsc -p tsconfig-compiled.json && gulp libs", "rollup": "rollup -f iife -c -o dist/bundle.es2015.js", "es5": "tsc --target es5 --allowJs dist/bundle.es2015.js --out dist/bundle.js", "build_prod": "npm run ngc && npm run build && npm run rollup && npm run es5 && npm run minify && gulp clean:build && gulp build_prod", "minify": "uglifyjs dist/bundle.js --screw-ie8 --compress --mangle --output dist/bundle.min.js",
any help would be welcome!
原文:https://stackoverflow.com/questions/39900994
最满意答案
在
ClientThread
的run()
方法中,实现:MainActivity.this.runOnUiThread(new Runnable() { public void run() { rcvtext.setText(tcpclient.getdata()); } });
这告诉Android只要它可以在UI线程(主应用程序线程)上运行它,而不是当前线程。
Within your
ClientThread
'srun()
method, implement this:MainActivity.this.runOnUiThread(new Runnable() { public void run() { rcvtext.setText(tcpclient.getdata()); } });
That tells Android to run it on the UI thread (the main application thread) whenever it can, rather than the current thread.
相关问答
更多-
看看NSThread API文档 。 有这样的方法 - (BOOL)isMainThread + (BOOL)isMainThread 和+ (NSThread *)mainThread Have a look at the NSThread API documentation. There are methods like - (BOOL)isMainThread + (BOOL)isMainThread and + (NSThread *)mainThread
-
makeObjectsPerformSelector:总是发生在主线程上吗?(Does makeObjectsPerformSelector: always happen on the main thread?)[2022-08-05]
不必要。 方法将在它们被调用的线程上运行,除非文档另有指定。 比较performSelectorOnMainThread:withObject:waitUntilDone:和performSelector:onThread:withObject:waitUntilDone:modes: Not necessarily. Methods will run on the thread where they're invoked unless otherwise specified by their docs. ... -
您可以在后台线程上查询并创建一个可传递给虚拟机的ThreadSafeReference 。 例如: var reference = await Task.Run(() => { using (var realm = Realm.GetInstance()) { var modelToPass = realm.All
().Where(...).FirstOrDefault(); return ThreadSafeReference.Create ... -
您可以实现自定义Interface ,以便从主活动中处理它。 在SampleThread : public interface TextViewChangeListener { public void onTextViewChanged(String newName); } TextViewChangeListener mListener; 然后在TextView任何位置调用mListener.onTextViewChanged(String newName) 。 请记住mListener使用Ma ...
-
超级方法(pushViewController:animated :)无法覆盖,所以你应该像下面的代码。 [self performSelectorOnMainThread:@selector(pushMyViewController:) withObject:[NSDictionary dictionaryWithObjectsAndKeys:<#yourViewController#>,@"viewController", [NSNumber numberWithBool:YES], @"animate ...
-
您正在从与创建它的位置不同的线程访问managedObjectContext。 这是核心数据线程规则#1。 您正在从应用程序代理获取MOC。 如果它是普通的Xcode生成的MOC,那么它是使用线程约束并发创建的。 你甚至不能用它调用performBlock。 您只能从主线程访问该MOC。 期。 其他任何事情最好都是用火来玩。 如果你想在一个单独的线程中完成所有的工作,你还需要一个单独的MOC。 像这样(只需键入 - 不编译)... NSManagedObjectContext *moc = [[NSMana ...
-
tl; dr - 虽然我找不到官方文档,但当前的实现确实确保UIViewController dealloc发生在主线程上。 我想我可以给出一个简单的答案,但也许我今天可以做一点“教一个人钓鱼”。 好。 我无法在任何地方找到这方面的文件,我也不记得它曾经公开过。 事实上,我一直都在努力确保视图控制器在主线程上被释放,这是我第一次见到有人指出UIViewController对象在主线程上自动释放。 也许别人可以找到官方声明,但我找不到一个。 但是,我确实有一些证据证明它确实发生了。 实际上,起初,我认为你没有 ...
-
如何处理线程和主线程更新listitem时的冲突(How to process conflict on updating listitem by a thread and main thread)[2022-02-04]
通常,您不希望从2个不同的线程中操作数据。 至少,并非没有保护。 另外,您无法修改非UI线程上的视图。 更新视图的后备数据(不是视图本身,只是视图所代表的项目)并调用notifyDatasetChanged() 。 该列表将在此时与适配器一起使用以重新呈现视图,此时适配器可以相应地更新视图。 In general, you don't want to be manipulating your data from 2 different threads. At least, not without prote ... -
在ClientThread的run()方法中,实现: MainActivity.this.runOnUiThread(new Runnable() { public void run() { rcvtext.setText(tcpclient.getdata()); } }); 这告诉Android只要它可以在UI线程(主应用程序线程)上运行它,而不是当前线程。 Within your ClientThread's run() method, implement this: ...
-
如果你想让QQuickView存在于main()线程之外,那么你必须: 创建一个新的std::thread (不是QThread ,因为它是一个QObject因此不能在QGuiApplication之前创建它) 在该线程中运行init()函数。 让它实例化你的QGuiApplication并启动事件循环。 在该线程中创建您的QQuickView 。 确保仅从该线程访问所有 GUI对象。 确保main()线程在您的其他线程中创建QGuiApplication之后才创建任何QObject 。 有关更多详细信息, ...