vue-class-component使用问题

vue-class-component使用问题

<script lang="ts">

import { defineComponent, ref, nextTick, unref, onMounted } from 'vue';

import { useScript } from '/@/hooks/web/useScript';

const BAI_DU_MAP_URL = 'https://api.map.baidu.com/getscript?v=3.0&ak=xxx';

export default defineComponent({

setup() {

const wrapRef = ref<HTMLDivElement | null>(null);

const { toPromise } = useScript({ src: BAI_DU_MAP_URL });

async function initMap() {

await toPromise();

await nextTick();

const wrapEl = unref(wrapRef);

if (!wrapEl) return;

const BMap = (window as any).BMap;

const map = new BMap.Map(wrapEl);

const point = new BMap.Point(116.404, 39.915);

map.centerAndZoom(point, 15);

map.enableScrollWheelZoom(true);

}

onMounted(() => {

initMap();

});

return { wrapRef };

},

})

</script>

vue-class-component写法

<script lang="ts">

import { ref, nextTick, unref } from 'vue'

import {Vue} from 'vue-property-decorator'

import { useScript } from '/@/hooks/web/useScript'

const BAI_DU_MAP_URL = 'https://api.map.baidu.com/getscript?v=3.0&ak=xxx';

export default class LogisticsDetail extends Vue {

private wrapRef = ref<HTMLDivElement | null>(null)

private toPromise = useScript({ src: BAI_DU_MAP_URL })

async initMap() {

await this.toPromise();

await nextTick();

const wrapEl = unref(this.wrapRef);

if (!wrapEl) return;

const BMap = (window as any).BMap;

const map = new BMap.Map(wrapEl);

const point = new BMap.Point(116.404, 39.915);

map.centerAndZoom(point, 15);

map.enableScrollWheelZoom(true);

}

mounted() {

this.initMap()

}

}

</script>

错误提示: Uncaught (in promise) TypeError: this.toPromise is not a function,这个要怎么改?


回答:

我给你翻译翻译“Uncaught (in promise) TypeError: this.toPromise is not a function”

未在promise中捕获的类型异常:this.toPromise不是一个函数

根据你前面写的,useScript返回一个对象,对象包含一个toPromise方法,你改了之后起码也是this.toPromise.toPromise才行啊???

你真的有仔细写代码吗???

以上是 vue-class-component使用问题 的全部内容, 来源链接: utcz.com/p/935873.html

回到顶部