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,这个要怎么改?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我给你翻译翻译“Uncaught (in promise) TypeError: this.toPromise is not a function”
未在promise中捕获的类型异常:this.toPromise不是一个函数
根据你前面写的,useScript返回一个对象,对象包含一个toPromise方法,你改了之后起码也是this.toPromise.toPromise才行啊???
你真的有仔细写代码吗???