如何在Webpack中使用快捷方式路径“ @”?

ise*_*xxx 5 npm webpack vue.js

我用package.json进行了“ npm run build”。我听了这条消息。如何在Webpack中使用@?

./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector中的错误。js?type = script&index = 0!./ src / App.vue找不到模块:错误:无法解析'C:\ Users \ ctc \ Downloads \ vue-navbar \ src'@中的'@ / components / CompHoge' ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?typ e = script&index = 0!./ src / App.vue 11:0-45 @ ./src/App。 Vue @ ./src/main.js

但是在“ npm run dev”中,它成功了。我的package.json:

"scripts": {
  "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
  ...
  "build": "cross-env NODE_ENV=production webpack --progress --hide-modules",
  ...
},
...
Run Code Online (Sandbox Code Playgroud)

有了这个package.json,它成功了:

"build": "node build/build.js",
Run Code Online (Sandbox Code Playgroud)

2月6日。添加了我的webpack.config.js:

...
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            // Since sass-loader (weirdly) has SCSS as its default parse mode, we map
            // the "scss" and "sass" values for the lang attribute to the right configs here.
            // other preprocessors should work out of the box, no loader config like this necessary.
            'scss': [
              'vue-style-loader',
              'css-loader',
              'sass-loader'
            ],
            'sass': [
              'vue-style-loader',
              'css-loader',
              'sass-loader?indentedSyntax'
            ]
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
...
Run Code Online (Sandbox Code Playgroud)

Ben*_*lis 5

要将“ @”用作路径根,如果使用的是标准vue cli创建的项目(或将“ src”指向组件所在的源根),则需要在webpack.config.js中添加resolve部分。 ):

resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      '@': resolve('src'),
    }
  }
  
Run Code Online (Sandbox Code Playgroud)

如果您正在使用vue-cli 3,则@已设置为引用src(请参阅:https : //github.com/vuejs/vue-cli/blob/ff57b8f55fa69873f643e418cfe6d4842d7c7674/packages/%40vue/cli-service/lib/ config / base.js#L49-L50),因此无需更改配置即可使用。