首先 新建env
其次 在nuxt.config.ts中配置
export default defineNuxtConfig({runtimeConfig: {// 公共变量,会被打包到客户端public: {showInsuranceMenu: process.env.SHOW_INSURANCE_MENU === 'true', // 读取环境变量showOtherMenus: process.env.SHOW_OTHER_MENUS === 'true',},},
})
最后 在页面中使用
const {public: { showInsuranceMenu, showOtherMenus }} = useRuntimeConfig();// 路由监听:只有当仅仅是保险的页面的时候,当点击/跳转的时候,重新跳转到保险的页面watch(() => route,val => {if (showInsuranceMenu && !showOtherMenus) {if (val.path == "/") {router.push("/insurance/life");}}},{ immediate: true, deep: true });