z_mango 2021-04-15 21:33 采纳率: 66.7%
浏览 650

VUE 在mounted()内调用通过CDN方式加载的外部JS资源时,发现未加载完怎么办?

VUE项目,有一个页面需要引入第三方CDN,为了不污染全局,我没有在index.html里引入,而是在加载该页面时写入Script标签,把该CDN引入

  created(){
    const s = document.createElement("script");
    s.type = "text/javascript";
    s.src =
      "https://apis.map.qq.com/tools/geolocation/min?key=mykxxxxxey&referer=myApp";
    document.head.appendChild(s);
    // document.getElementsByTagName('head')[0].appendChild(s)
  },

然后在mounted内使用这个CDN的资源,发现报错(应该是这个外部资源,没有加载完成)

  mounted() {
    var geolocation = new qq.maps.Geolocation();
    geolocation.getIpLocation(showPosition, showErr);

    function showPosition(position) {
      console.log(position);
    }

    function showErr() {
    .......
    }
  },

这种情况怎么办?

我只想在该页面时引入这个CDN,然后在mounted里调用,该怎么解决?

  • 写回答

4条回答 默认 最新

  • 张小帅和刘美美 2021-04-15 21:37
    关注

    这块代码 改为同步吧 等这个cdn 加载完 在往下走
     

    评论

报告相同问题?