简体   繁体   English

Vue JS 3 应用程序可在旧版 Firefox/Chrome 中运行

[英]Vue JS 3 Application to run in older Firefox/Chrome

I'm developing an app in VueJS 3 and it seems that's working on my local computer.我正在用 VueJS 3 开发一个应用程序,它似乎在我的本地计算机上运行。 But I have to make it runs also on older browsers like Firefox 38 Chrome 49.但我必须让它也可以在 Firefox 38 Chrome 49 等旧版浏览器上运行。

My app uses some "fetch" functions to load content from api, to authenticate, to send commands, etc. After reading the following 2 links I believe I have some issues with it (despite it should works on CH 42).我的应用程序使用一些“获取”功能从 api 加载内容、进行身份验证、发送命令等。阅读以下 2 个链接后,我相信我有一些问题(尽管它应该适用于 CH 42)。 https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API and Babel not Polyfilling Fetch when using babel-preset-env .使用 babel-preset-env 时https://developer.mozilla.org/en-US/docs/Web/API/Fetch_APIBabel 不是 Polyfilling Fetch What it is your opinion?你的意见是什么?

I have babel and in package.json the following settings:我有通天塔,在 package.json 中有以下设置:

"browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead",
    "Chrome > 48",
 ]

But the errors received in Firefox and Chrome are very cryptic and I don't know what how should I transpile that function.但是在 Firefox 和 Chrome 中收到的错误非常神秘,我不知道应该如何编译 function。 I would focus on Chrome 49 and I'll add some context我将专注于 Chrome 49,并添加一些上下文

Indeed the async function is available after Chrome 55. But I don't know how to convert it https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function实际上异步 function 在 Chrome 55 之后可用。但我不知道如何转换它https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

For example in Chrome 49 I have the following console error on the following page: webpack:///./node_modules/@vue/devtools-api/esm/proxy.js例如,在 Chrome 49 中,我在以下页面上有以下控制台错误: webpack:///./node_modules/@vue/devtools-api/esm/proxy.js

Uncaught SyntaxError: Unexpected identifier proxy.js:94未捕获的 SyntaxError:意外的标识符 proxy.js:94

The content of that file is:该文件的内容是:

import { HOOK_PLUGIN_SETTINGS_SET } from './const';
export class ApiProxy {
constructor(plugin, hook) {
    this.target = null;
    this.targetQueue = [];
    this.onQueue = [];
    this.plugin = plugin;
    this.hook = hook;
    const defaultSettings = {};
    if (plugin.settings) {
        for (const id in plugin.settings) {
            const item = plugin.settings[id];
            defaultSettings[id] = item.defaultValue;
        }
    }
    const localSettingsSaveId = `__vue-devtools-plugin-settings__${plugin.id}`;
    let currentSettings = Object.assign({}, defaultSettings);
    try {
        const raw = localStorage.getItem(localSettingsSaveId);
        const data = JSON.parse(raw);
        Object.assign(currentSettings, data);
    }
    catch (e) {
        // noop
    }
    this.fallbacks = {
        getSettings() {
            return currentSettings;
        },
        setSettings(value) {
            try {
                localStorage.setItem(localSettingsSaveId, JSON.stringify(value));
            }
            catch (e) {
                // noop
            }
            currentSettings = value;
        },
    };
    if (hook) {
        hook.on(HOOK_PLUGIN_SETTINGS_SET, (pluginId, value) => {
            if (pluginId === this.plugin.id) {
                this.fallbacks.setSettings(value);
            }
        });
    }
    this.proxiedOn = new Proxy({}, {
        get: (_target, prop) => {
            if (this.target) {
                return this.target.on[prop];
            }
            else {
                return (...args) => {
                    this.onQueue.push({
                        method: prop,
                        args,
                    });
                };
            }
        },
    });
    this.proxiedTarget = new Proxy({}, {
        get: (_target, prop) => {
            if (this.target) {
                return this.target[prop];
            }
            else if (prop === 'on') {
                return this.proxiedOn;
            }
            else if (Object.keys(this.fallbacks).includes(prop)) {
                return (...args) => {
                    this.targetQueue.push({
                        method: prop,
                        args,
                        resolve: () => { },
                    });
                    return this.fallbacks[prop](...args);
                };
            }
            else {
                return (...args) => {
                    return new Promise(resolve => {
                        this.targetQueue.push({
                            method: prop,
                            args,
                            resolve,
                        });
                    });
                };
            }
        },
    });
}
async setRealTarget(target) {
    this.target = target;
    for (const item of this.onQueue) {
        this.target.on[item.method](...item.args);
    }
    for (const item of this.targetQueue) {
        item.resolve(await this.target[item.method](...item.args));
    }
}
}

I tried to change anything I found:我试图改变我发现的任何东西:

vue.config.js vue.config.js

    transpileDependencies: [
    "config",
    "vue",
    "vue-router",
    "vuex",
    "xml2js"
],

package.json package.json

"dependencies": {
"@babel/polyfill": "^7.12.1",
"babel-polyfill": "^6.26.0",
"config": "^3.3.7",
"core-js": "^3.6.5",
"regenerator-runtime": "^0.13.9",
"vue": "^3.2.26",
"vue-router": "^4.0.12",
"vuex": "^4.0.2",
"xml2js": "^0.4.23"
  },
  "devDependencies": {
"@babel/cli": "^7.16.8",
"@babel/core": "^7.16.12",
"@babel/preset-env": "^7.16.11",
"@vue/babel-preset-app": "^4.5.15",
"@vue/cli-plugin-babel": "~4.5.0",
"@vue/cli-plugin-eslint": "~4.5.0",
"@vue/cli-plugin-router": "~4.5.0",
"@vue/cli-service": "~4.5.0",
"@vue/compiler-sfc": "^3.0.0",
"babel-eslint": "^10.1.0",
"eslint": "^6.8.0",
"eslint-plugin-vue": "^7.0.0"
  },
  "browserslist": [
"defaults",
"> 1%",
"last 2 versions",
"not dead",
"Chrome > 48"
  ]

babel.config.js babel.config.js

module.exports = {
presets: [
  [
      '@vue/cli-plugin-babel/preset',
      {
          useBuiltIns: "usage",
          forceAllTransforms: true,
          targets: {
              "chrome": "49"
          },
          }
  ]
  ]
};

main.js main.js

import "@babel/polyfill";
import 'core-js/stable';
import 'regenerator-runtime/runtime';
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './_store'
createApp(App).use(router).use(store).mount('#app');

i setup a new vue 3 project and added async/await and fetch and did a build and it workes in chrome 48我设置了一个新的 vue 3 项目并添加了 async/await 和 fetch 并进行了构建,它在 chrome 48 中工作

I modified hello world component to我将 hello world 组件修改为

<script>
export default {
  name: 'HelloWorld',
  data() {
    return {msg : ""}
  },
  mounted() {

    async function fetchMoviesJSON() {
      const response = await fetch('https://reqbin.com/echo/get/json');
      const movies = await response.json();
      return movies;
    }

    fetchMoviesJSON().then(data => {
      this.msg = data; // fetched movies
    });


  }
}
</script>

added "Chrome > 47" to browserslist"Chrome > 47"添加到browserslist列表

also i installed browserslist我也安装了browserslist

  1. can you run after installing browserslist npx browserslist and check if you see chrome 47 there?你可以在安装 browserslist npx browserslist后运行并检查你是否在那里看到 chrome 47 吗?
  2. I saw in your error dev-tools i think it might be just the Vue dev tools我在您的错误dev-tools中看到了我认为它可能只是 Vue 开发工具

Update: added vue router and installed the vue dev tools and when i run with serve i can see that the proxy file was compiled there and no errors.更新:添加了 vue 路由器并安装了 vue 开发工具,当我使用serve运行时,我可以看到代理文件在那里编译并且没有错误。

are you building it in dev mode?你是在开发模式下构建它吗?

I would recommend you to clean up your babel plugins and try with the simple way.我建议你清理你的 babel 插件并尝试使用简单的方法。 also please let know the node/nom version.还请告知节点/标称版本。 i read it might be the problem, and you should try to update node see here我读到这可能是问题所在,您应该尝试更新节点,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM