细分分析提供了一个包含秘密 API 密钥的代码片段。在我的Nuxt.js项目中,我创建了一个名为的插件segment.js,我在我的nuxt.config.js:
nuxt.config.js
plugins: [
{
src: "~/plugins/segment.js",
mode: 'client'
}
]
Run Code Online (Sandbox Code Playgroud)
在我的plugins/segment.js文件中我有我的片段:
!function(){var analytics=window.analytics=...analytics.SNIPPET_VERSION="4.13.2";
analytics.load(process.env.SEGMENT_API_SECRET);
analytics.page();
}}();
Run Code Online (Sandbox Code Playgroud)
显然我不想让我的秘密 API 密钥暴露在那里,所以我把它存储在我的.env文件中:
.env
SEGMENT_API_SECRET=FR4....GSDF3S
Run Code Online (Sandbox Code Playgroud)
问题:process.env.SEGMENT_API_SECRET因此该片段不起作用。如何从我的插件访问我的变量?plugins/segment.jsundefined.envSEGMENT_API_SECRETplugins/segment.js
我不想遵循将环境变量(.env文件)放在 Nuxt 项目的根目录中的约定。
How can I achieve another directory or even name for it without using the @nuxtjs/dotenv module? (I know this one is already baked into Nuxt since v2.13 and hence, not needed anymore).