参考:vue组件中未定义require

发布于 2025-02-09 02:57:42 字数 3180 浏览 2 评论 0原文

当我在vue组件中使用screetfull.js(incortfull')时,会发生以下错误,我该怎么办,请帮助我。 错误信息

verion:

vue:2.6.14

@vue/vue/vue/cli - 服务:5.0.4

babel-loader:8.2.5

vue-loader:17.0.0

webpack:5.73.0

eslint:7.32.0

config:

babel.config.config.js

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset'
  ],
  env: {
    development: {
      plugins: ['dynamic-import-node']
    }
  },
  plugins: ['lodash']
}

vue.config.config.js

const {defineConfig} = require('@vue/cli-service')
const path = require('path')
const webpack = require('webpack')

function resolve(dir) {
  return path.join(__dirname, dir)
}

const name = process.env.VUE_APP_TITLE || 'hello'

module.exports = defineConfig({
  transpileDependencies: true,
  publicPath: process.env.NODE_ENV === 'development' ? '/' : '/',
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: process.env.NODE_ENV !== 'production',
  productionSourceMap: false,
  configureWebpack: {
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    plugins: [
      new webpack.IgnorePlugin({
        resourceRegExp: /\.\/locale/,
        contextRegExp: /moment/
      })
    ]
  },
  chainWebpack(config) {
    config.plugins.delete('preload')
    config.plugins.delete('prefetch')

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/assets/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/assets/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    config.when(process.env.NODE_ENV !== 'development', config => {
      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial' 
          },
          elementUI: {
            chunks: 'all',
            name: 'chunk-elementUI', 
            priority: 20, 
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/ 
          },
          commons: {
            chunks: 'all',
            name: 'chunk-commons',
            test: resolve('src/components'), 
            minChunks: 3,
            priority: 5,
            reuseExistingChunk: true
          },
          echarts: {
            chunks: 'all',
            test: /echarts/,
            name: 'echarts',
            enforce: true
          },
          lodash: {
            chunks: 'all',
            test: /lodash/,
            name: 'lodash',
            enforce: true
          },
          fabric: {
            chunks: 'all',
            test: /fabric/,
            name: 'fabric',
            enforce: true
          }
        }
      })
    })
  }
})

The following error occurs when I use screenfull.js(import screenfull from "screenfull") in a Vue component, what should I do, help me please.
error info

version:

vue: 2.6.14

@vue/cli-service: 5.0.4

babel-loader: 8.2.5

vue-loader: 17.0.0

webpack: 5.73.0

eslint: 7.32.0

config:

babel.config.js

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset'
  ],
  env: {
    development: {
      plugins: ['dynamic-import-node']
    }
  },
  plugins: ['lodash']
}

vue.config.js

const {defineConfig} = require('@vue/cli-service')
const path = require('path')
const webpack = require('webpack')

function resolve(dir) {
  return path.join(__dirname, dir)
}

const name = process.env.VUE_APP_TITLE || 'hello'

module.exports = defineConfig({
  transpileDependencies: true,
  publicPath: process.env.NODE_ENV === 'development' ? '/' : '/',
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: process.env.NODE_ENV !== 'production',
  productionSourceMap: false,
  configureWebpack: {
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    plugins: [
      new webpack.IgnorePlugin({
        resourceRegExp: /\.\/locale/,
        contextRegExp: /moment/
      })
    ]
  },
  chainWebpack(config) {
    config.plugins.delete('preload')
    config.plugins.delete('prefetch')

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/assets/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/assets/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    config.when(process.env.NODE_ENV !== 'development', config => {
      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial' 
          },
          elementUI: {
            chunks: 'all',
            name: 'chunk-elementUI', 
            priority: 20, 
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/ 
          },
          commons: {
            chunks: 'all',
            name: 'chunk-commons',
            test: resolve('src/components'), 
            minChunks: 3,
            priority: 5,
            reuseExistingChunk: true
          },
          echarts: {
            chunks: 'all',
            test: /echarts/,
            name: 'echarts',
            enforce: true
          },
          lodash: {
            chunks: 'all',
            test: /lodash/,
            name: 'lodash',
            enforce: true
          },
          fabric: {
            chunks: 'all',
            test: /fabric/,
            name: 'fabric',
            enforce: true
          }
        }
      })
    })
  }
})

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

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

发布评论

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

评论(1

流年已逝 2025-02-16 02:57:42

vue.config.js您有transpiledepentencies已启用,尝试将其删除或排除包:

module.exports = defineConfig({
  transpileDependencies: false
})

In vue.config.js you have transpileDependencies enabled, try to remove it or exclude the package:

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