English 中文(简体)
• 如何在网上包装中混淆道路?
原标题:How to configure path alias in webpack.mix?

<>Solution:

Thanks to both Karl & Ali I can now use alias without having to create a seperate webpack.config.js file. Just add the alias to webpack.mix.js like so:

const mix = require( laravel-mix );
const path = require( path );

mix.js( resources/js/app.js ,  public/js )
    .postCss( resources/css/app.css ,  public/css )
    .alias({ @ :  resources/ })
    .webpackConfig({resolve: {alias: { @ : path.resolve( resources/ )}}})
    .vue();

< Question:

From this post, I learned that you could configure your @ path in webpack.mix.js to represent the assets/resources folder in a vue/laravel project so that you can use the @ symbol in your paths.

mix.webpackConfig({
  resolve: {
    alias: {
       @resources : path.resolve( resources ),
    },
  },
})

不幸的是,这导致了以下错误。

yarn run v1.22.17 $ mix watch [webpack-cli] ReferenceError: path is not defined at Object. (/Users/artur/PhpstormProjects/safa-ameedee.com/webpack.mix.js:16:21) at Module._compile (node:internal/modules/cjs/loader:1097:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10) at Module.load (node:internal/modules/cjs/loader:975:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Module.require (node:internal/modules/cjs/loader:999:19) at require (node:internal/modules/cjs/helpers:102:18) at module.exports (/Users/artur/PhpstormProjects/safa-ameedee.com/node_modules/laravel-mix/setup/webpack.config.js:11:5) at loadConfigByPath (/Users/artur/PhpstormProjects/safa-ameedee.com/node_modules/webpack-cli/lib/webpack-cli.js:1747:27) at async Promise.all (index 0) error Command failed with exit code 2.

我尝试了不同的改动(@assets等),但我总是有同样的错误。 因此,我做了什么错误?

最佳回答

在您的网站上:

...
.alias({ @ :  resources/js })
...

Import Aliases - Laracasts

问题回答

错误信息表明,没有界定<代码>path模块。 该模块是用于处理和改造档案道路的核心单元。 您可在<条码>上查询。 类似档案:

const mix = require( laravel-mix );
const path = require( path );

mix.js( resources/js/app.js ,  public/js )
   .postCss( resources/css/app.css ,  public/css , [
        //
    ])
   .webpackConfig({
       resolve: {
           alias: {
                @ : path.resolve(__dirname,  resources/js ),
           },
       },
   });

In this code, const path = require( path ); requires the path module, and path.resolve(__dirname, resources/js ) generates an absolute path to the resources/js directory. The @ alias now represents this directory, and you can use it in your import statements.





相关问题
Nuxt 3 useRoute import cannot find

I am using latest nuxt version and when I tried to use the useRoute method is triggers a "Cannot Find name useRoute" but it works so I would like to know what could I be missing My package ...

Vue array dependent select not display data

I have dependent selection like this. Selection image when i console log, they have data but not render data in selection data This is what i ve tried. for Product selection has default selected value ...

How to inject Bearer Token in Axios boot file

Quasar has a different setup using boot files and my current axios.ts looks liek this: import { boot } from quasar/wrappers ; import axios, { AxiosInstance } from axios ; declare module @vue/...

Vue router and Vite Base url conflict

I use Vue Router to develop my single page application and use Vite to package my project. I want to deploy all static files (such as index.js and index.css) under another CDN domain name (cdn.example....

Vue.js change class on mounted

I have a button element with a .hover property and I need the hover animation to also happen as soon as the page is mounted. How can I attribute a class to a button on the mounted function and then ...

Laravel +Vuejs CORS issue

I am facing a dilemma wit the CORS issue. I have application that is configured as follow: API: https://abc.mydomain.com (laravel) front end: https://subdomain.mydomain.com(VueJS) Both applications ...

热门标签