Search Docs
Collection Mixins 是为扩展 Collection 类提供的一种机制,可以通过 dataSourceManager.addCollectionMixins() 添加 Collection Mixins。
dataSourceManager.addCollectionMixins()
1import { Collection, Plugin } from '@tachybase/client'; 2 3class TestMixin extends Collection { 4 test() { 5 const { name } = this.options; 6 return 'test '+ name; 7 } 8} 9 10class MyPlugin extends Plugin { 11 async load() { 12 this.app.dataSourceManager.addCollectionMixins([TestMixin]); 13 } 14}
CollectionManager
getCollection()
Collection
1const Demo = () => { 2 const cm = useCollectionManager(); 3 const userCollection = cm.getCollection<TestMixin>('users'); 4 5 userCollection.test(); // 'test users' 6}
useCollection()
1const Demo = () => { 2 const collection = useCollection<TestMixin>(); 3 collection.test(); // 'test users' 4}
如果添加了 Mixins,可通过如下方式获得类型提示:
1const Demo = () => { 2 const collection = useCollection<TestMixin & Test2Mixin>(); 3}