使用 webpack 导入 vuejs 路由器组件

发布于 2025-01-13 18:51:36 字数 1532 浏览 0 评论 0原文

在 vuejs 中,当我像这样导入路由器中的组件时: import('@/layout') 它工作正常并且符合预期。如果必须对结果进行字符串化,它会看起来像这样:

function ()
{
   return Promise.resolve().then
   (
          function ()
          {
              return Object(C_Users_Admin_Documents_GitHub_MyProject_node_modules_babel_runtime_helpers_esm_interopRequireWildcard__WEBPACK_IMPORTED_MODULE_1__["default"])(__webpack_require__(/*! @/layout */ "./src/layout/index.vue"));
          }
   );
}

另一方面,当我动态地输入字符串部分时,如下所示: import(route.meta.componentPath) 它不会解析并且看起来像这样:

function ()
{
   return Promise.resolve("".concat(route.meta.componentPath)).then
   (
          function (s)
          {
             return Object(C_Users_Admin_Documents_GitHub_MyProject_node_modules_babel_runtime_helpers_esm_interopRequireWildcard__WEBPACK_IMPORTED_MODULE_1__["default"])(__webpack_require__("./src/store/modules sync recursive")(s));
          }
   );
}

注意解析和webpack_require_部分(向右滚动

我该如何让后者工作?我对 webpack 完全陌生,但所有错误/答案都指向它,对吗?我所设置的只是 babel.config.js ,如下所示:

module.exports = {
      presets: [
        '@vue/cli-plugin-babel/preset'
      ],
      'env': {
        'development': {
          // babel-plugin-dynamic-import-node plugin converts all import() to require().
          // This plugin can significantly increase the speed of hot updates, when you have a large number of pages.
          'plugins': ['dynamic-import-node']
        }
      }
    }

我也不能 100% 确定它在下面的作用。

In vuejs, when I import a component in the router as such: import('@/layout') it works fine and as expected. If one had to stringify the outcome, it would look like this:

function ()
{
   return Promise.resolve().then
   (
          function ()
          {
              return Object(C_Users_Admin_Documents_GitHub_MyProject_node_modules_babel_runtime_helpers_esm_interopRequireWildcard__WEBPACK_IMPORTED_MODULE_1__["default"])(__webpack_require__(/*! @/layout */ "./src/layout/index.vue"));
          }
   );
}

On the other hand when I feed in the string part dynamically as such: import(route.meta.componentPath) it does not resolve and looks like this:

function ()
{
   return Promise.resolve("".concat(route.meta.componentPath)).then
   (
          function (s)
          {
             return Object(C_Users_Admin_Documents_GitHub_MyProject_node_modules_babel_runtime_helpers_esm_interopRequireWildcard__WEBPACK_IMPORTED_MODULE_1__["default"])(__webpack_require__("./src/store/modules sync recursive")(s));
          }
   );
}

Notice the difference in the resolve and webpack_require_ parts (SCROLL TO THE RIGHT)

How shall I get the latter to work? I am totally new to webpack but all errors/answers are pointing to it right? All I have set up is babel.config.js as follows:

module.exports = {
      presets: [
        '@vue/cli-plugin-babel/preset'
      ],
      'env': {
        'development': {
          // babel-plugin-dynamic-import-node plugin converts all import() to require().
          // This plugin can significantly increase the speed of hot updates, when you have a large number of pages.
          'plugins': ['dynamic-import-node']
        }
      }
    }

which I also am not 100% sure what it does underneath.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2025-01-20 18:51:36

如果您只是用此函数替换整个导入,它就可以工作:

function (resolve) {
  require([route.meta.componentPath], resolve);
}

但是后面的代码到目前为止与原始代码不同。我并不是将此标记为答案,而是作为一种解决方法,因为真正知道它是如何正确完成的会很好。

It works if you just replace the entire import with this function:

function (resolve) {
  require([route.meta.componentPath], resolve);
}

however the code behind is by far not the same as the original. I'm not marking this as an answer but as a workaround as it would be nice to actually know how it's done properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文