本文整理匯總了TypeScript中vuex.Store.subscribe方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Store.subscribe方法的具體用法?TypeScript Store.subscribe怎麽用?TypeScript Store.subscribe使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類vuex.Store
的用法示例。
在下文中一共展示了Store.subscribe方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: async
return async (store: Store<State>) => {
const water: Partial<State> = {};
for (const paths of Object.values(saveOn)) {
for (const path of castArray(paths)) {
const storedValue = storage.getItem(prefixKey(path));
if (storedValue) {
const parsedValue = JSON.parse(storedValue);
set(water, path, parsedValue);
}
}
}
store.replaceState(merge({}, store.state, water));
if (onLoad) await onLoad(store);
store.subscribe(({ type }, state) => {
if (hasOwn(saveOn, type)) {
const paths = castArray(saveOn[type]);
for (const [path, value] of zip(paths, at(state, paths))) {
if (path === undefined) continue;
const stringifiedValue = JSON.stringify(value);
try {
storage.setItem(prefixKey(path), stringifiedValue);
} catch {
// this can sometimes throw based on the spec
}
}
}
});
};
示例2:
const localStoragePlugin: Plugin<RootState> = (store: Store<RootState>) => {
store.subscribe((mutation: MutationPayload, { patch: { name }}) => {
localStorage.setItem(_NAME, name);
});
store.subscribe((mutation: MutationPayload, { patch: { modules }}) => {
localStorage.setItem(_MODULES, JSON.stringify(modules));
});
store.subscribe((mutation: MutationPayload, { patch: { connections }}) => {
localStorage.setItem(_CONNECTIONS, JSON.stringify(connections));
});
store.subscribe((mutation: MutationPayload, { patch: { parameterSets }}) => {
localStorage.setItem(_PARAMETERS, JSON.stringify(parameterSets));
});
store.subscribe((mutation: MutationPayload, { patch: { parameterKey }}) => {
localStorage.setItem(_PARAMETER_KEY, parameterKey.toString());
});
store.subscribe((mutation: MutationPayload, { patch: { id }}) => {
localStorage.setItem(_ID, id.toString());
});
store.subscribe((mutation: MutationPayload, { app: { patchKey }}) => {
localStorage.setItem(_KEY, patchKey);
});
};
示例3: createStore
c: {
state: { count: 4 },
mutations
}
}
});
}
namespace TestSubscribe {
const store = createStore();
const handler = (mutation: Vuex.MutationObject<any>, state: ISimpleState) => {
state.count += 1;
};
const unsubscribe = store.subscribe(handler);
unsubscribe();
}
namespace TestLogger {
const logger = createLogger<ISimpleState>({
collapsed: false,
transformer: state => state.count,
mutationTransformer: m => m
});
new Vuex.Store<ISimpleState>({
state: { count: 1 },
plugins: [logger]
});
}
示例4: return
return (handler: any) => {
return store.subscribe(handler);
};