【问题标题】:Handle Dynamic import failure in vue处理 vue 中的动态导入失败
【发布时间】:2019-12-10 17:53:21
【问题描述】:

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

我需要处理这种情况:

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"
  ]
}

【问题讨论】:

    标签: vue.js dynamic import


    【解决方案1】:

    你可以使用它的承诺:

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

    【讨论】:

    猜你喜欢
    • 2018-06-07
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多