useEntity
用于响应式加载Entity
,当数据变化时自动销毁或重载entity实例
Usage
vue
<script setup lang="ts">
import * as Cesium from 'cesium';
import { useEntity, useViewer } from 'vesium';
import { shallowRef, watchPostEffect } from 'vue';
// use entity instance
const entity1 = useEntity(new Cesium.Entity({
position: Cesium.Cartesian3.fromDegrees(150, 10),
label: {
font: '14px Arial',
text: 'entity instance',
},
}));
// use getter
const entity2 = useEntity(() => {
return new Cesium.Entity({
position: Cesium.Cartesian3.fromDegrees(150, 9),
label: {
font: '14px Arial',
text: 'use getter',
},
});
});
const entityRef = shallowRef(new Cesium.Entity({
position: Cesium.Cartesian3.fromDegrees(150, 8),
label: {
font: '14px Arial',
text: 'use ref',
},
}));
// use ref
const entity3 = useEntity(entityRef);
// use array
const entities = useEntity([
new Cesium.Entity({
position: Cesium.Cartesian3.fromDegrees(149, 7),
label: {
font: '14px Arial',
text: 'array item 1',
},
}),
new Cesium.Entity({
position: Cesium.Cartesian3.fromDegrees(151, 7),
label: {
font: '14px Arial',
text: 'array item 2',
},
}),
]);
const viewer = useViewer();
watchPostEffect(() => {
if (entity1.value && entity2.value && entity3.value && entities.value) {
viewer.value?.flyTo([
entity1.value,
entity2.value,
entity3.value,
...entities.value!,
], {
duration: 1,
});
}
});
</script>
<template>
</template>
ts
// overLoad1:加载单项实例
const entity = useEntity(entity);
// overLoad2:加载数组实例
const entities = useEntity([entity1, entity2]);
const isActive = ref(true);
const isLoading = ref(true);
// 配置项
const entity = useEntity(entity, {
collection,
isActive,
evaluating: isLoading
});
Type Definitions
typescript
import type { Entity, EntityCollection } from 'cesium';
import type { ComputedRef, MaybeRefOrGetter, Ref } from 'vue';
import type { MaybeRefOrAsyncGetter } from '../toPromiseValue';
export interface UseEntityOptions {
/**
* The collection of Entity to be added
* @default useViewer().value.entities
*/
collection?: EntityCollection;
/**
* default value of `isActive`
* @default true
*/
isActive?: MaybeRefOrGetter<boolean>;
/**
* Ref passed to receive the updated of async evaluation
*/
evaluating?: Ref<boolean>;
}
/**
* Add `Entity` to the `EntityCollection`, automatically update when the data changes, and destroy the side effects caused by the previous `Entity`.
*
* overLoaded1: Parameter supports passing in a single value.
*/
export declare function useEntity<T extends Entity = Entity>(entity?: MaybeRefOrAsyncGetter<T | undefined>, options?: UseEntityOptions): ComputedRef<T | undefined>;
/**
* Add `Entity` to the `EntityCollection`, automatically update when the data changes, and destroy the side effects caused by the previous `Entity`.
*
* overLoaded2: Parameter supports passing in an array.
*/
export declare function useEntity<T extends Entity = Entity>(entities?: MaybeRefOrAsyncGetter<Array<T | undefined>>, options?: UseEntityOptions): ComputedRef<T[] | undefined>;