百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

Vue3 require is not defined(is not defined翻译)

ztj100 2025-07-23 19:29 6 浏览 0 评论

vue2中想动态引入资源,而且涉及到if else 的判断条件的 ,类似于vue/index.js 中的

if (process.env.NODE_ENV === 'production') {
  module.exports = require('./dist/vue.cjs.prod.js')
} else {
  module.exports = require('./dist/vue.cjs.js')
}

而这个require是内部封装。我们项目中又不是直接引用。 而且用 import 又不能用到if else 的局部中

采用 import.meta.globEager 方法代替require导入资源。比如下面根据的引入mockjs ,可以把这段放在main.js中

// 非生产环境, 适配mockjs模拟数据                 // api: https://github.com/nuysoft/Mock
if (process.env.NODE_ENV !== 'production') {
    //debugger
    const allmockfiles= import.meta.globEager(  '@/api/allmock.js')
}

而不是要引用某个目录下的文件的话,比如我下面的中英文

//提示信息仅供在开发环境生效
import { createI18n } from 'vue-i18n';
import store from '@/store'
const files= import.meta.globEager(  './modules/*.js')
let messages ={}
Object.keys(files).forEach((c )=> {
    const module = files[c].default
    const moduleName =c.replace( /^\.\/(.*)\/(.*)\.\w+$/, '$2')
    messages[moduleName] = module
})

//const lang = store.state.app.lang || navigator.userLanguage || navigator.language // 初次进入,采用测览器当前设置的语言,然认采用中文
const lang = navigator.userLanguage || navigator.language
const locale = lang.indexOf('en')!== -1 ? 'en':'zh-cn'
const i18n = createI18n({
    legacy: false,
    __VUE_I18N_LEGACY_API__: false,
    __VUE_I18N_FULL_INSTALL__: false,
    locale: locale,
    fallbackLocale:'zh-cn',
    messages
})
document.querySelector( 'html' ).setAttribute( 'lang', locale)

export default i18n

其中用到的加载的部分核心就是,要的人也可以在forEach方法里面自己加判断条件

const files= import.meta.globEager(  './modules/*.js')
let messages ={}
Object.keys(files).forEach((c )=> {
    const module = files[c].default
    const moduleName =c.replace( /^\.\/(.*)\/(.*)\.\w+$/, '$2')
    messages[moduleName] = module
})

相关推荐

10条军规:电商API从数据泄露到高可用的全链路防护

电商API接口避坑指南:数据安全、版本兼容与成本控制的10个教训在电商行业数字化转型中,API接口已成为连接平台、商家、用户与第三方服务的核心枢纽。然而,从数据泄露到版本冲突,从成本超支到系统崩溃,A...

Python 文件处理在实际项目中的困难与应对策略

在Python项目开发,文件处理是一项基础且关键的任务。然而,在实际项目中,Python文件处理往往会面临各种各样的困难和挑战,从文件格式兼容性、编码问题,到性能瓶颈、并发访问冲突等。本文将深入...

The Future of Manufacturing with Custom CNC Parts

ThefutureofmanufacturingisincreasinglybeingshapedbytheintegrationofcustomCNC(ComputerNumericalContro...

Innovative Solutions in Custom CNC Machining

Inrecentyears,thelandscapeofcustomCNCmachininghasevolvedrapidly,drivenbyincreasingdemandsforprecisio...

C#.NET serilog 详解(c# repository)

简介Serilog是...

Custom CNC Machining for Small Batch Production

Inmodernmanufacturing,producingsmallbatchesofcustomizedpartshasbecomeanincreasinglycommondemandacros...

Custom CNC Machining for Customized Solutions

Thedemandforcustomizedsolutionsinmanufacturinghasgrownsignificantly,drivenbydiverseindustryneedsandt...

Revolutionizing Manufacturing with Custom CNC Parts

Understandinghowmanufacturingisevolving,especiallythroughtheuseofcustomCNCparts,canseemcomplex.Thisa...

Breaking Boundaries with Custom CNC Parts

BreakingboundarieswithcustomCNCpartsinvolvesexploringhowadvancedmanufacturingtechniquesaretransformi...

Custom CNC Parts for Aerospace Industry

Intherealmofaerospacemanufacturing,precisionandreliabilityareparamount.Thecomponentsthatmakeupaircra...

Cnc machining for custom parts and components

UnderstandingCNCmachiningforcustompartsandcomponentsinvolvesexploringitsprocesses,advantages,andcomm...

洞察宇宙(十八):深入理解C语言内存管理

分享乐趣,传播快乐,增长见识,留下美好。亲爱的您,这里是LearingYard学苑!今天小编为大家带来“深入理解C语言内存管理”...

The Art of Crafting Custom CNC Parts

UnderstandingtheprocessofcreatingcustomCNCpartscanoftenbeconfusingforbeginnersandevensomeexperienced...

Tailored Custom CNC Solutions for Automotive

Intheautomotiveindustry,precisionandefficiencyarecrucialforproducinghigh-qualityvehiclecomponents.Ta...

关于WEB服务器(.NET)一些经验累积(一)

以前做过技术支持,把一些遇到的问题累积保存起来,现在发出了。1.问题:未能加载文件或程序集“System.EnterpriseServices.Wrapper.dll”或它的某一个依赖项。拒绝访问。解...

取消回复欢迎 发表评论: