本文整理汇总了TypeScript中mobx-state-tree.types.union方法的典型用法代码示例。如果您正苦于以下问题:TypeScript types.union方法的具体用法?TypeScript types.union怎么用?TypeScript types.union使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mobx-state-tree.types
的用法示例。
在下文中一共展示了types.union方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: test
test("can time travel with Mutable object", t => {
const MutableUnion = types.union(
types.string,
types.boolean,
types.number,
types.map(types.late(() => MutableUnion)),
types.array(types.late(() => MutableUnion))
)
const MutableStoreModel = types
.model({
mutable: MutableUnion
})
.actions(self => {
setUndoManagerDifferentTree(self)
return {
setProp(k, v) {
self.mutable.set(k, v)
}
}
})
const store = MutableStoreModel.create({ mutable: {} })
const mutable = store.mutable
t.deepEqual(mutable.toJSON(), {})
t.is(undoManager.canUndo, false)
t.is(undoManager.canRedo, false)
t.is(undoManager.history.length, 0)
store.setProp("foo", 1)
t.deepEqual(mutable.toJSON(), { foo: 1 })
t.is(undoManager.canUndo, true)
t.is(undoManager.canRedo, false)
t.is(undoManager.history.length, 1)
store.setProp("foo", {})
t.deepEqual(mutable.toJSON(), { foo: {} })
t.is(undoManager.canUndo, true)
t.is(undoManager.canRedo, false)
t.is(undoManager.history.length, 2)
undoManager.undo()
t.deepEqual(mutable.toJSON(), { foo: 1 })
t.is(undoManager.canUndo, true)
t.is(undoManager.canRedo, true)
t.is(undoManager.history.length, 2)
undoManager.undo()
t.deepEqual(mutable.toJSON(), {})
t.is(undoManager.canUndo, false)
t.is(undoManager.canRedo, true)
t.is(undoManager.history.length, 2)
})
示例2: currentUrl
import { types } from 'mobx-state-tree';
import { toJS } from 'mobx';
import { toUrlParams } from 'app/core/utils/url';
const QueryInnerValueType = types.union(types.string, types.boolean, types.number);
const QueryValueType = types.union(QueryInnerValueType, types.array(QueryInnerValueType));
export const ViewStore = types
.model({
path: types.string,
query: types.map(QueryValueType),
routeParams: types.map(QueryValueType),
})
.views(self => ({
get currentUrl() {
let path = self.path;
if (self.query.size) {
path += '?' + toUrlParams(toJS(self.query));
}
return path;
},
}))
.actions(self => {
// querystring only
function updateQuery(query: any) {
self.query.clear();
for (let key of Object.keys(query)) {
if (query[key]) {
self.query.set(key, query[key]);
}
示例3: currentUrl
import { types } from 'mobx-state-tree';
const QueryValueType = types.union(types.string, types.boolean, types.number);
const urlParameterize = queryObj => {
const keys = Object.keys(queryObj);
const newQuery = keys.reduce((acc: string, key: string, idx: number) => {
const preChar = idx === 0 ? '?' : '&';
return acc + preChar + key + '=' + queryObj[key];
}, '');
return newQuery;
};
export const ViewStore = types
.model({
path: types.string,
query: types.map(QueryValueType),
})
.views(self => ({
get currentUrl() {
let path = self.path;
if (self.query.size) {
path += urlParameterize(self.query.toJS());
}
return path;
},
}))
.actions(self => {
function updateQuery(query: any) {
self.query.clear();