Skip to main content

功能标志

Backstage 提供在插件内或应用程序创建过程中定义功能标志的功能。 这样,您就可以将插件的部分功能限制给将功能标志切换为 "开启 "的单个用户。

本页介绍了定义设置和读取特征标志的过程。 如果您想将特征标志与软件模板一起使用,可在以下页面找到写作模板.

定义特征标志

在插件中

在插件中定义特征标志的方法是将特征标志的名称传递到featureFlags阵列

/* src/plugin.ts */
import { createPlugin, createRouteRef } from '@backstage/core-plugin-api';
import ExampleComponent from './components/ExampleComponent';

export const examplePlugin = createPlugin({
id: 'example',
routes: {
root: rootRouteRef,
},
featureFlags: [{ name: 'show-example-feature' }],
});

在应用程序中

在应用程序中定义特征标志的方法是在featureFlags数组中createApp()功能调用:

const app = createApp({
// ...
featureFlags: [
{
pluginId: '', // pluginId is required for feature flags in plugins. It can be left blank for a feature flag leveraged in the application.
name: 'tech-radar',
description: 'Enables the tech radar plugin',
},
],
// ...
});

启用功能标志

功能标志默认为关闭,用户可在Backstage界面自行更新。

这些设置可通过导航至以下页面进行Settings>Feature Flags.

用户的选择会保存在浏览器的本地存储空间中,一旦切换,用户可能需要刷新页面才能看到任何新的更改。

FeatureFlagged 组件

根据特征标志的状态控制内容的最简单方法是使用功能标记组件。

import { FeatureFlagged } from '@backstage/core-app-api';

...

<FeatureFlagged with="show-example-feature">
<NewFeatureComponent />
</FeatureFlagged>

<FeatureFlagged without="show-example-feature">
<PreviousFeatureComponent />
</FeatureFlagged>

评估特征标志状态

还可以使用功能标志 Api.

import { useApi, featureFlagsApiRef } from '@backstage/core-plugin-api';

const featureFlagsApi = useApi(featureFlagsApiRef);
const isOn = featureFlagsApi.isActive('show-example-feature');