【问题标题】:JavaScript function works only on Windows machineJavaScript 函数仅适用于 Windows 机器
【发布时间】:2021-11-30 06:56:17
【问题描述】:

我正在我的 Windows 计算机以及我的 Macbook 上处理一个项目。我有一个函数,可以像这样注册全局某些目录中定义的组件:

require('./bootstrap');

import { createApp, h } from 'vue';
import { createInertiaApp } from '@inertiajs/inertia-vue3';
import { InertiaProgress } from '@inertiajs/progress';

/**
 * Third-party imports.
 */
import upperFirst from 'lodash/upperFirst';
import camelCase from 'lodash/camelCase';

const appName = window.document.getElementsByTagName('title')[0]?.innerText || 'Laravel';

/**
 * Register components from the ./components and ./components/core
 * directories. This way these components don't have to be imported
 * manually every time.
 *
 * The core components are the core of other components and contains
 * alerts, notifications, input fields etc.
 *
 * Base components are bigger components made upon the core components
 * and contain a combination of one or more of the core components. These
 * are usually components that contain a lot of code and have to be re-used
 * across the whole application.
 *
 * @param {Vue instance} app
 */
function registerGlobalComponents (app) {
  const requireComponent = require.context(
    './components' || './components/core' || './components/base',
    true,
    /\.vue$/
  );

  for (const file of requireComponent.keys()) {
    const componentConfig = requireComponent(file);
    const name = file
      .replace(/index.js/, '')
      .replace(/^\.\//, '')
      .replace(/\.\w+$/, '');
    const componentName = upperFirst(camelCase(name));

    app.component(componentName,
      componentConfig.default);
  }
}

createInertiaApp({
    title: (title) => `${title} - ${appName}`,
    resolve: (name) => require(`./Pages/${name}.vue`),
    setup({ el, app, props, plugin }) {

        const inertiaApp = createApp({ render: () => h(app, props) });

        inertiaApp.use(plugin);
        inertiaApp.mixin({ methods: { route } })
        registerGlobalComponents(inertiaApp);
        inertiaApp.mount(el);
    },
});

InertiaProgress.init({ color: '#4B5563' });

当我运行 npm run watchdevprod 时,它会编译代码并成功地在我的 Windows 机器上显示网站。但是当我在我的 Macbook 上执行此操作时,它会引发以下错误:

无法解析“/Users/bas/Sites/trainfit/resources/js”中的“./components”

这有什么理由可以在 Windows 机器上运行,但不能在运行 MacOS 的机器上运行?

关于我的项目的更多信息:

  • 使用 Vue 和 Inertiajs 制作
  • 从 Visual Studio Code 运行此代码

【问题讨论】:

  • 您的代码看起来不完整。 requirerequireComponent 对象是什么?在 Windows/MacOS 中运行是什么意思?您是在 Node、浏览器中运行,还是在什么地方运行?你错过了一些额外的标签吗?
  • Windows和MacOS的文件夹结构一样吗?
  • @CascadiaJS 我的项目的文件夹结构是一样的。但是机器上项目的路径是不同的。 MacOS /var/www/Sites/projectname 和 Windows D://laragon/www/projectname
  • 无法解析错误表示,file not found错误

标签: javascript windows macos


【解决方案1】:

docs 中所述,首先在根目录中创建一个 jsconfig.json 文件,其中包含以下字符和符号:

{
  "compilerOptions": {
    "baseUrl": "src"
  },
  "include": ["src"]
}

您可以在此处找到示例和参考资料:-here

【讨论】:

  • 今晚在家的时候我会试试你的答案,如果成功了我会告诉你的。
  • 是的,请立即参考上面的博客。 ?
猜你喜欢
  • 2022-01-21
  • 2013-11-29
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-03
  • 1970-01-01
  • 2013-02-26
相关资源
最近更新 更多