Vue引入外部如何引入外部js文件

vue项目中直接引入在index.html引入static中的js文件往往会报错,如下图:

故解决方法如下:

1、在main.js中写入如下代码:

  Import Vue form 'vue'
    Vue.component(‘external-script’,{
	    Render :function(createElement){
		    var _this = this;
	        return  createElement(‘script’,{
	            attrs:{
	                type:'text/javascript',
	                src:this.src
                },
                on:{
	                load:function(event){
	                _this.$emit(‘load’,event);
                    },
                    error:function(event){
	                _this.$emit(‘error’,event);
                    },
                    readystatechange:function(event){
	                    if(this.readyState ==’complete’){
	                    _this.$emit(‘load’,event);
                        }
                    }
                }
            });
        },
        props:{
	        src:{
                type:String,,
                required:true
            }
        }
    })

2、vue项目文件中引入



例:
复制代码

即引入成功!!!不在报次错误

转载于:https://juejin.im/post/5c26245be51d4570c053ded8

你可能感兴趣的:(Vue引入外部如何引入外部js文件)