我已经为文章集合类型创建了预览按钮。我正在使用Strapi博客模板。我设法使预览按钮出现在Content 中。我硬编码要打开的链接,当你点击预览按钮,它可以工作。现在,我希望插件使用带有环境变量的链接,而不是硬编码链接。我不知道如何访问插件源代码中的环境变量。
我的目标:
我想替换一下
href={`http://localhost:3000?secret=abc&slug=${initialData.slug}`}
使用
href={${CLIENT_FRONTEND_URL}?secret=${CLIENT_SECRET}&slug=${initialData.slug}`}
在./src/plugins/previewbtn/admin/src/components/PreviewLink/index.js中
其中CLIENT_FRONTEND_URL
和CLIENT_SECRET
是在.env中声明的环境变量。
CLIENT_FRONTEND_URL=http://localhost:3000
CLIENT_PREVIEW_SECRET=abc
下面是我使用的代码的一个概要:
首先,我使用博客模板创建了一个strapi应用程序,然后创建了一个插件。
// Create strapi app named backend with a blog template
$ yarn create strapi-app backend --quickstart --template @strapi/template-blog@1.0.0 blog && cd backend
// Create plugin
$ yarn strapi generate
接下来,我创建了一个PreviewLink文件来提供预览按钮的链接
// ./src/plugins/previewbtn/admin/src/components/PreviewLink/index.js
import React from 'react';
import { useCMEditViewDataManager } from '@strapi/helper-plugin';
import Eye from '@strapi/icons/Eye';
import { LinkButton } from '@strapi/design-system/LinkButton';
const PreviewLink = () => {
const {initialData} = useCMEditViewDataManager();
if (!initialData.slug) {
return null;
}
return (
<LinkButton
size="S"
startIcon={<Eye/>}
style={{width: '100%'}}
href={`http://localhost:3000?secret=abc&slug=${initialData.slug}`}
variant="secondary"
target="_blank"
rel="noopener noreferrer"
title="page preview"
>Preview
</LinkButton>
);
};
export default PreviewLink;
然后,我只在bootstrap(app) { ... }
部分编辑了这个预生成的文件。
// ./src/plugins/previewbtn/admin/src/index.js
import { prefixPluginTranslations } from '@strapi/helper-plugin';
import pluginPkg from '../../package.json';
import pluginId from './pluginId';
import Initializer from './components/Initializer';
import PreviewLink from './components/PreviewLink';
import PluginIcon from './components/PluginIcon';
const name = pluginPkg.strapi.name;
export default {
register(app) {
app.addMenuLink({
to: `/plugins/${pluginId}`,
icon: PluginIcon,
intlLabel: {
id: `${pluginId}.plugin.name`,
defaultMessage: name,
},
Component: async () => {
const component = await import(/* webpackChunkName: "[request]" */ './pages/App');
return component;
},
permissions: [
// Uncomment to set the permissions of the plugin here
// {
// action: '', // the action name should be plugin::plugin-name.actionType
// subject: null,
// },
],
});
app.registerPlugin({
id: pluginId,
initializer: Initializer,
isReady: false,
name,
});
},
bootstrap(app) {
app.injectContentManagerComponent('editView', 'right-links', {
name: 'preview-link',
Component: PreviewLink
});
},
async registerTrads({ locales }) {
const importedTrads = await Promise.all(
locales.map(locale => {
return import(
/* webpackChunkName: "translation-[request]" */ `./translations/${locale}.json`
)
.then(({ default: data }) => {
return {
data: prefixPluginTranslations(data, pluginId),
locale,
};
})
.catch(() => {
return {
data: {},
locale,
};
});
})
);
return Promise.resolve(importedTrads);
},
};
最后,它创建了这个文件来启用插件参考文献。
// ./config/plugins.js
module.exports = {
// ...
'preview-btn': {
enabled: true,
resolve: './src/plugins/previewbtn' // path to plugin folder
},
// ...
}
发布于 2022-11-20 06:57:55
您不必更改webpack配置,只需在根目录add中找到.env文件
AWS_ACCESS_KEY_ID = your key here
然后由accessKeyId: env('AWS_ACCESS_KEY_ID')
导入
https://stackoverflow.com/questions/73153359
复制相似问题