重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
umbraco-state-management by umbraco/umbraco-cms-backoffice-skills
npx skills add https://github.com/umbraco/umbraco-cms-backoffice-skills --skill umbraco-state-managementUmbraco 中的状态是响应式值的容器,它使用观察者模式实现跨组件实例的通信。Umbraco 状态是一个值的容器,你可以从中创建可观察对象,允许多个观察者订阅并在状态改变时自动接收更新。这种模式对于在上下文和元素之间共享数据而无需紧密耦合特别有用。
在实现前请务必获取最新文档:
import { UmbStringState } from '@umbraco-cms/backoffice/observable-api';
// 使用初始值创建状态
const myState = new UmbStringState('initial value');
// 从状态创建一个可观察对象
const myObservable = myState.asObservable();
// 观察状态(立即触发并在变化时触发)
this.observe(myObservable, (value) => {
console.log('Current value:', value);
});
// 更新状态(所有观察者都会收到通知)
myState.setValue('updated value');
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
import { UmbContextBase } from '@umbraco-cms/backoffice/class-api';
import { UmbNumberState } from '@umbraco-cms/backoffice/observable-api';
export class MyContext extends UmbContextBase<MyContext> {
// 私有状态
#counter = new UmbNumberState(0);
// 公共可观察对象(只读)
readonly counter = this.#counter.asObservable();
increment() {
this.#counter.setValue(this.#counter.getValue() + 1);
}
decrement() {
this.#counter.setValue(this.#counter.getValue() - 1);
}
}
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
import { MY_CONTEXT } from './my-context.js';
export class MyElement extends UmbLitElement {
@state()
private _count = 0;
constructor() {
super();
this.consumeContext(MY_CONTEXT, (context) => {
// 观察上下文中的计数器状态
this.observe(
context.counter,
(count) => {
this._count = count;
},
'_countObserver'
);
});
}
render() {
return html`
<div>Count: ${this._count}</div>
`;
}
}
import {
UmbStringState,
UmbNumberState,
UmbBooleanState,
UmbArrayState,
UmbObjectState
} from '@umbraco-cms/backoffice/observable-api';
// 字符串状态
const name = new UmbStringState('John');
// 数字状态
const age = new UmbNumberState(25);
// 布尔状态
const isActive = new UmbBooleanState(true);
// 数组状态
const items = new UmbArrayState(['item1', 'item2']);
// 对象状态
const user = new UmbObjectState({ name: 'John', age: 25 });
import { UmbArrayState } from '@umbraco-cms/backoffice/observable-api';
const itemsState = new UmbArrayState(['apple', 'banana', 'cherry']);
// 仅观察第一个项目
const firstItem = itemsState.asObservablePart(data => data?.[0]);
// 仅观察数量
const itemCount = itemsState.asObservablePart(data => data.length);
// 在元素中使用
this.observe(firstItem, (first) => {
console.log('First item:', first);
});
this.observe(itemCount, (count) => {
console.log('Total items:', count);
});
import { UmbArrayState } from '@umbraco-cms/backoffice/observable-api';
const listState = new UmbArrayState<string>([]);
// 添加项目
listState.setValue([...listState.getValue(), 'new item']);
// 移除项目
listState.setValue(
listState.getValue().filter(item => item !== 'old item')
);
// 清空所有
listState.setValue([]);
// 获取当前值
const current = listState.getValue();
import { UmbContextBase } from '@umbraco-cms/backoffice/class-api';
import { UmbStringState, UmbArrayState } from '@umbraco-cms/backoffice/observable-api';
export class TodoContext extends UmbContextBase<TodoContext> {
#title = new UmbStringState('My Todo List');
#todos = new UmbArrayState<string>([]);
readonly title = this.#title.asObservable();
readonly todos = this.#todos.asObservable();
readonly todoCount = this.#todos.asObservablePart(data => data.length);
setTitle(value: string) {
this.#title.setValue(value);
}
addTodo(todo: string) {
this.#todos.setValue([...this.#todos.getValue(), todo]);
}
removeTodo(todo: string) {
this.#todos.setValue(
this.#todos.getValue().filter(t => t !== todo)
);
}
}
状态 : 值的容器(私有,可变)
可观察对象 : 从状态创建的订阅钩子(公共,只读)
观察者 : 通过 observe() 对状态变化做出反应的函数
状态类型 :
UmbStringState - 文本值UmbNumberState - 数值UmbBooleanState - 布尔标志UmbArrayState - 集合UmbObjectState - 复杂对象UmbClassState - 类实例可观察部分 : 仅当映射值改变时才更新的派生可观察对象
最佳实践 : 保持状态私有,公开暴露可观察对象
使用场景 :
就是这样!请务必获取最新文档,保持示例简洁,生成完整可运行的代码。
每周安装次数
69
仓库
GitHub 星标数
14
首次出现
2026年2月4日
安全审计
安装于
github-copilot50
cursor23
opencode21
codex21
gemini-cli19
amp19
States in Umbraco are containers for reactive values that enable communication across component instances using the Observable pattern. An Umbraco State is a container for a value that you can create Observables from, which allows multiple observers to subscribe and automatically receive updates when the state changes. This pattern is particularly useful for sharing data between contexts and elements without tight coupling.
Always fetch the latest docs before implementing:
import { UmbStringState } from '@umbraco-cms/backoffice/observable-api';
// Create a state with initial value
const myState = new UmbStringState('initial value');
// Create an observable from the state
const myObservable = myState.asObservable();
// Observe the state (fires immediately and on changes)
this.observe(myObservable, (value) => {
console.log('Current value:', value);
});
// Update the state (all observers notified)
myState.setValue('updated value');
import { UmbContextBase } from '@umbraco-cms/backoffice/class-api';
import { UmbNumberState } from '@umbraco-cms/backoffice/observable-api';
export class MyContext extends UmbContextBase<MyContext> {
// Private state
#counter = new UmbNumberState(0);
// Public observable (readonly)
readonly counter = this.#counter.asObservable();
increment() {
this.#counter.setValue(this.#counter.getValue() + 1);
}
decrement() {
this.#counter.setValue(this.#counter.getValue() - 1);
}
}
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
import { MY_CONTEXT } from './my-context.js';
export class MyElement extends UmbLitElement {
@state()
private _count = 0;
constructor() {
super();
this.consumeContext(MY_CONTEXT, (context) => {
// Observe the counter state from context
this.observe(
context.counter,
(count) => {
this._count = count;
},
'_countObserver'
);
});
}
render() {
return html`
<div>Count: ${this._count}</div>
`;
}
}
import {
UmbStringState,
UmbNumberState,
UmbBooleanState,
UmbArrayState,
UmbObjectState
} from '@umbraco-cms/backoffice/observable-api';
// String state
const name = new UmbStringState('John');
// Number state
const age = new UmbNumberState(25);
// Boolean state
const isActive = new UmbBooleanState(true);
// Array state
const items = new UmbArrayState(['item1', 'item2']);
// Object state
const user = new UmbObjectState({ name: 'John', age: 25 });
import { UmbArrayState } from '@umbraco-cms/backoffice/observable-api';
const itemsState = new UmbArrayState(['apple', 'banana', 'cherry']);
// Observe just the first item
const firstItem = itemsState.asObservablePart(data => data?.[0]);
// Observe just the count
const itemCount = itemsState.asObservablePart(data => data.length);
// Use in element
this.observe(firstItem, (first) => {
console.log('First item:', first);
});
this.observe(itemCount, (count) => {
console.log('Total items:', count);
});
import { UmbArrayState } from '@umbraco-cms/backoffice/observable-api';
const listState = new UmbArrayState<string>([]);
// Add item
listState.setValue([...listState.getValue(), 'new item']);
// Remove item
listState.setValue(
listState.getValue().filter(item => item !== 'old item')
);
// Clear all
listState.setValue([]);
// Get current value
const current = listState.getValue();
import { UmbContextBase } from '@umbraco-cms/backoffice/class-api';
import { UmbStringState, UmbArrayState } from '@umbraco-cms/backoffice/observable-api';
export class TodoContext extends UmbContextBase<TodoContext> {
#title = new UmbStringState('My Todo List');
#todos = new UmbArrayState<string>([]);
readonly title = this.#title.asObservable();
readonly todos = this.#todos.asObservable();
readonly todoCount = this.#todos.asObservablePart(data => data.length);
setTitle(value: string) {
this.#title.setValue(value);
}
addTodo(todo: string) {
this.#todos.setValue([...this.#todos.getValue(), todo]);
}
removeTodo(todo: string) {
this.#todos.setValue(
this.#todos.getValue().filter(t => t !== todo)
);
}
}
State : Container for a value (private, mutable)
Observable : Subscription hook created from state (public, readonly)
Observer : Function that reacts to state changes via observe()
State Types :
UmbStringState - text valuesUmbNumberState - numeric valuesUmbBooleanState - boolean flagsUmbArrayState - collectionsUmbObjectState - complex objectsUmbClassState - class instancesObservable Parts : Derived observables that only update when mapped value changes
Best Practice : Keep states private, expose observables publicly
Use Cases :
That's it! Always fetch fresh docs, keep examples minimal, generate complete working code.
Weekly Installs
69
Repository
GitHub Stars
14
First Seen
Feb 4, 2026
Security Audits
Gen Agent Trust HubFailSocketPassSnykPass
Installed on
github-copilot50
cursor23
opencode21
codex21
gemini-cli19
amp19
HeroUI v2 到 v3 迁移指南:破坏性变更、复合组件、Tailwind v4 升级
556 周安装