与巴贝尔发生反应.我对import和module.exports感到困惑.在将ES6代码转换为ES5时,我假设babel将导入和导出分别转换为require和module.exports.
如果我从一个模块导出一个函数并在另一个模块中导入该函数,代码执行正常.但是如果我使用module.exports导出函数并使用"import"导入,则会在运行时抛出错误,表示它不是函数.
我做了一个例子.
// Tiger.js
function Tiger() {
function roar(terrian){
console.log('Hey i am in ' + terrian + ' and i am roaing');
};
return roar;
}
module.exports = Tiger;
// animal.js
import { Tiger } from './animals';
var animal = Tiger();
animal("jungle");
Run Code Online (Sandbox Code Playgroud)
我使用预设es2015的babel进行反编译.这给了我以下错误
未捕获的TypeError:(0,_ animals.Tiger)不是函数
但是如果我删除它module.exports = Tiger;并用它替换export { Tiger };它工作正常.
我在这里失踪了什么?
编辑: 我使用browserify作为模块捆绑器.
我有一个全局的HttpInterceptor,带有一个处理HttpErrorResponse的catch块。但是我的要求是,当服务进行http调用并且还具有错误处理程序时,我希望该服务上的错误处理程序首先退出。如果此服务上没有错误处理程序,那么我希望全局HttpInterceptor错误处理程序对其进行处理。
示例代码:
Http拦截器:
@Injectable()
export class ErrorHttpInterceptor implements HttpInterceptor {
constructor(private notificationService: NotificationService) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req)
.catch(
error => {
if (error instanceof HttpErrorResponse) {
this.notificationService.error('Error', 'Error in handling Http request');
}
return Observable.empty<HttpEvent<any>>();
}
);
}
}
Run Code Online (Sandbox Code Playgroud)
和服务电话:
updateUser(id, data) {
return this.http
.patch(`${API.userUrl}/${id}`, {data: data})
.subscribe(
() => console.log('success'),
(err) => this.notificationService.error('custom code to handle this')
);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,ErrorHttpInterceptor给出通知,然后userService错误处理也给出错误通知。
但是在我的用例中,我只希望底层订阅未处理错误时,ErrorHttpIntercetor才能处理错误。有没有办法做到这一点?