useDataSource
用于响应式加载 Cesium DataSource
。当数据发生变化时,它会自动销毁或重载 DataSource
实例。
Usage
vue
<script setup lang="ts">
import * as Cesium from 'cesium';
import { useDataSource, useViewer } from 'vesium';
import { watchPostEffect } from 'vue';
import geojson from '../../.vitepress/static/geojson.json?url';
const dataSource = useDataSource(() => Cesium.GeoJsonDataSource.load(geojson));
const viewer = useViewer();
watchPostEffect(() => {
if (dataSource.value) {
viewer.value?.flyTo(
dataSource.value,
{
duration: 1,
},
);
}
});
</script>
<template>
</template>
vuesium
ts
// 加载基础实例
const dataSource = useDataSource(someDataSource);
// 异步加载
const dataSource = useDataSource(async () => await getDataSource());
// 加载数组
const dataSources = useDataSource([dataSource1, dataSource2]);
const isLoading = ref(true);
// 使用配置项
const dataSource = useDataSource(someDataSource, {
collection: someCollection, // 数据源集合
isActive: true, // 是否激活
evaluating: isLoading, // 加载状态引用
destroyOnRemove: true, // 当数据源移除时是否销毁
});
Type Definitions
typescript
import type { DataSourceCollection } from 'cesium';
import type { ComputedRef, MaybeRefOrGetter, Ref } from 'vue';
import type { MaybeRefOrAsyncGetter } from '../toPromiseValue';
import type { CesiumDataSource } from '../utils';
export interface UseDataSourceOptions {
/**
* The collection of DataSource to be added
* @default useViewer().value.dataSources
*/
collection?: DataSourceCollection;
/**
* default value of `isActive`
* @default true
*/
isActive?: MaybeRefOrGetter<boolean>;
/**
* Ref passed to receive the updated of async evaluation
*/
evaluating?: Ref<boolean>;
/**
* The second parameter passed to the `remove` function
*
* `dataSources.remove(dataSource,destroyOnRemove)`
*/
destroyOnRemove?: MaybeRefOrGetter<boolean>;
}
/**
* Add `DataSource` to the `DataSourceCollection`, automatically update when the data changes, and destroy the side effects caused by the previous `DataSource`.
*
* overload1: Parameter supports passing in a single value.
*
* @param dataSource The `DataSource` to added
* @param options additional options
*
* @returns computedRef of the `DataSource`
*
* @overload
*/
export declare function useDataSource<T extends CesiumDataSource = CesiumDataSource>(dataSource?: MaybeRefOrAsyncGetter<T | undefined>, options?: UseDataSourceOptions): ComputedRef<T | undefined>;
/**
* Add `DataSource` to the `DataSourceCollection`, automatically update when the data changes, and destroy the side effects caused by the previous `DataSource`.
*
* overload1: Parameter supports passing in an array.
*
* @param dataSources The list of `DataSource` to added
* @param options additional options
*
* @returns computedRef of the `DataSource`
*
* @overload
*/
export declare function useDataSource<T extends CesiumDataSource = CesiumDataSource>(dataSources?: MaybeRefOrAsyncGetter<T[] | undefined>, options?: UseDataSourceOptions): ComputedRef<T[] | undefined>;