处理Vue中的动态导入失败

时间:2019-08-02 06:08:02

标签: vue.js dynamic import

我必须导入一些应该覆盖标准组件的自定义组件。 这些自定义组件将环境变量加载到包含这些客户端的自定义组件的选定自定义文件夹中。 自定义组件不应该存在,所以我无法找到捕获动态导入失败的方法。

我需要处理这种情况:

const component = () => import('path_custom_not_existent..):
if(!component) {
component = () => import('path_standard_sure')
} 

所以我不能覆盖每个客户端的所有组件,而只能覆盖我需要的组件。

我尝试使用try / catch,但是没有用。 我尝试了import(...)。then()。catch(),但仍然无法正常工作。 每次我以错误的路径使用“导入”时,都会显示“找不到模块:错误:无法解析...”

我正在使用composer.json(vue-cli标准...)

{
  "name": "shockdom.front",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "axios": "^0.19.0",
    "bootstrap": "^4.3.1",
    "bootstrap-vue": "^2.0.0-rc.27",
    "core-js": "^2.6.5",
    "lodash": "^4.17.15",
    "vue": "^2.6.10",
    "vue-focus": "^2.1.0",
    "vue-router": "^3.0.7",
    "vuex": "^3.1.1",
    "vuex-persistedstate": "^2.5.4"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^3.9.2",
    "@vue/cli-plugin-eslint": "^3.9.2",
    "@vue/cli-service": "^3.9.3",
    "babel-eslint": "^10.0.1",
    "eslint": "^5.16.0",
    "eslint-plugin-vue": "^5.0.0",
    "node-sass": "^4.12.0",
    "sass-loader": "^7.1.0",
    "vue-template-compiler": "^2.6.10"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "rules": {},
    "parserOptions": {
      "parser": "babel-eslint"
    }
  },
  "postcss": {
    "plugins": {
      "autoprefixer": {}
    }
  },
  "browserslist": [
    "> 1%",
    "last 2 versions"
  ]
}

1 个答案:

答案 0 :(得分:0)

您可以使用承诺:

const component = () => import('path_custom_not_existent..)
  .then((m) => m.default)
  .catch(err) => import ('404_component_does_not_exist)