我正尝试在我的Angular & Nativescript项目中使用Apache Thrift,但通过'tns preview -bundle‘或我得到的任何其他tns命令
TypeError: Cannot read property 'browser' of undefined
File: "<unknown>, line: 1, column: 265Apache Thrift v.0.11.0/ 1.0.0dev、Angular v7.1.0、Nativescript v5.1.0
在platforms/android/app/src/main/assets/app/vendor.js中,我可以看到:
/* istanbul ignore next */
if (undefined.browser) {
defaultEncoding = 'utf-8'
} else {
var pVersionMajor = parseInt(undefined.version.split('.')[0].slice(1), 10)当我期望像global.process.browser这样的东西时,我可以在vendor.js的另一个地方找到它。在另一个和平中,我可以看到像undefined.nextTick(...)之类的东西。我知道这是某种巴别塔问题,但我不知道如何解决它。
我使用thrift --gen js:node脚本来生成我的thrift文件(这在错误中没有意义)。如果没有它们,所有的工作都很酷,但是当我尝试执行任何由thrift生成的文件/任何来自thrift的模块时,我得到了上面的一个异常。my_component.ts
import { Component, OnInit } from '@angular/core';
import { TJSONProtocol } from 'thrift'; // or anything else
@Component({...});
export class MyComponent implements OnInit {
ngOnInit () {
// console.log(TJSONProtocol); // Uncomment this string to get an exception.
}
constructor() {...};
}是否存在ng serve提供的任何问题。我希望这里有任何方法来修复这个错误,并使节俭与本机脚本兼容,或任何其他使用节俭与本机脚本的方式。
发布于 2018-12-27 22:23:30
问题出在Nativescript提供的默认webpack.config.js中。在:
plugins: [
// Define useful constants like TNS_WEBPACK
new webpack.DefinePlugin({
"global.TNS_WEBPACK": "true",
"process": undefined,
})您需要删除行"process": undefined。
发布于 2018-12-27 21:36:40
当应用程序使用浏览器数据(文档、窗口等)时,您需要确保仅当应用程序在浏览器中运行时才能访问这些数据,而nodejs并非如此。
要执行此操作,请将需要使用的代码片段放在检查中,如下所示:
if (typeof window !== 'undefined') {
// here runs your code...
console.log(TJSONProtocol);
}请参阅:https://angular.io/guide/universal#working-around-the-browser-apis
https://stackoverflow.com/questions/53945922
复制相似问题