【问题标题】:React-Native - Can't find variable : ProxyReact-Native - 找不到变量:代理
【发布时间】:2018-05-12 22:46:51
【问题描述】:

我正在使用 Proxy 作为我的 react native 应用程序

import Setting from "./Setting";
const lang = { ar : {...} , en : {...} , fr : {...} };

export const string = new Proxy(lang, {get: function (object, name){return object[Setting.settings.lang][name]}});
export default string;

但是 throws 找不到变量 Proxy

【问题讨论】:

    标签: react-native


    【解决方案1】:

    我无法添加评论,但我认为您的问题与同一个问题有关:- Proxy ES6

    换句话说,您需要导入一个 polyfill。

    --

    编辑添加代码:

    import 'proxy-polyfill';
    .
    .
    _samplePolyfill = () => {
      function observe(o, callback) {
        return new Proxy(o, {
          set(target, property, value) {
            callback(property, value);
            target[property] = value;
          },
        });
      }
    
      const x = {'name': 'BB-8'};
      const p = observe(x, (property, value) => console.warn(property, value));
      p.name = 'BB-9';
    }
    .
    .
    componentDidMount() {
      this._samplePolyfill();
    }
    

    【讨论】:

    • 已更新,导入需要是某种不是代理的名称。发现你需要旧版本,你可以在网上找到它的问题。所以运行 npm:npm install proxy-polyfill@v0.1.6 希望这会有所帮助。
    【解决方案2】:

    出于一个奇怪的原因,如果您启用 RemoteJs 调试器它可以工作

    Ctrl/CMD + M

    如果您在连接到远程调试器时遇到问题,请按照以下步骤操作: Unable to connect with remote debugger

    【讨论】:

    • 启用调试器后,您的 JS 在 chrome 上运行(这就是它“远程”的原因)。您的 chrome 具有 Proxy 对象。移动 JS 引擎,由设备上的 react native 使用,没有。
    猜你喜欢
    • 2018-05-28
    • 2018-10-26
    • 1970-01-01
    • 2021-10-10
    • 2017-01-24
    • 2019-05-27
    • 2021-11-13
    • 2021-05-31
    • 1970-01-01
    相关资源
    最近更新 更多