第一种方式:利用Vue的mounted生命周期

const oScript = document.createElement('script');
oScript.type = 'text/javascript';
oScript.src = '//g.alicdn.com/sd/smartCaptcha/0.0.1/index.js';
document.body.appendChild(oScript);

 第二种方式:利用Vue的createElement方法

components: {
    'scriptLink': {
      render(createElement) {
        return createElement(
          'script',
          {
            attrs: {
              type: 'text/javascript',
              src: '//g.alicdn.com/sd/smartCaptcha/0.0.1/index.js',
            },
          },
        )
      }
    }
  }

 然后通过<scriptLink></scriptLink>  引入到组件中

第三种方式:封装一个remoteJs 组件 

<template>
    <remote-js src="//g.alicdn.com/sd/smartCaptcha/0.0.1/index.js"></remote-js>
</template>
<script>
export default {
    components: {
    'remote-js': {
      render(createElement) {
        return createElement('script', {attrs: {type: 'text/javascript', src: this.src}});
      },
      props: {
        src: { type: String, required: true}
      }
    }
  }
}
</script>

 第四种方式:通过webpack-require-http插件

安装

npm install webpack-require-http

 webpack.base.config.js配置

{
    ...
    entry:{
        ...
    },
    output:{
        ...
    },
    externals:[
        require('webpack-require-http')
    ]
    ...
}

 在vue组件中使用

require(['http://pv.sohu.com/cityjson?ie=utf-8'], () => {
      setTimeout(() => {
        if (returnCitySN) {
          console.log(returnCitySN)
        } else {
        }
      }, 1500)
    })

 

Logo

前往低代码交流专区

更多推荐