webpack配置ProvidePlugin后,在使用时将不再需要import和require进行引入,直接使用即可。

使用方法:

在webpack.dev.conf.js和webpack.prod.conf.js中添加

1 plugins: [
2     ...
3     new webpack.ProvidePlugin({
4       'api': 'api'
5     }),
6     ...
7 ]

使用场景:

将所有接口调用方法放在 src/api/index.js中,在组件中无需import即可调用

相关配置:

在webpack.base.conf.js中添加

1  resolve: {
2     extensions: ['.js', '.vue', '.json'],
3     alias: {
4       'vue$': 'vue/dist/vue.esm.js',
5       '@': resolve('src'),
6       'api': resolve('src/api/index.js'),
7     }
8  },

在组件中使用:

1 ...
2 mounted() {
3     api.getData().then(res => {
4       this.items = res
5     }).catch(err => {
6       console.log(err)
7     })
8 },
9 ...

 

相关文章:

  • 2022-01-22
  • 2021-10-21
  • 2021-09-21
  • 2022-01-18
  • 2022-12-23
  • 2021-12-27
  • 2021-10-31
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-01
  • 2021-06-15
  • 2021-07-16
  • 2021-09-17
  • 2021-08-21
  • 2021-05-30
相关资源
相似解决方案