Problem
Recently in the use of Vue to do something, use the nail scan login function, here need to introduce remote JS file, because the way Vue is not the same as before, and do not want to download the file to the local application, find a solution, seemingly all need to introduce a third-party library, and finally found a solution to share.
Ideas
The first idea was to mounted
insert the remote script file in the body using JavaScript scripts after Vue loaded the Dom ().
Later, the Vue method was discovered createElement
, simply encapsulating a component to solve the problem.
Workaround
The first version of the code (directly in the Operation Dom) is as follows:
ExportDefault{Mounted(){ConstS=document. Createelement ( ' script ' s. Type = ' text/javascript ' s. src = https://g.alicdn.com/dingding/dinglogin/0.0.2/ Ddlogin.js ' document. Body. Appendchild (s},}
How to use createElement
:
ExportDefault{Components:{' Dingtalk ':{Render(CreateElement){Returncreateelement ( ' script ' {attrs: {type: Text/javascript ' src: ' https://g.alicdn.com/dingding/dinglogin/0.0.2/ddLogin.js ' }, }, }, },} Use <dingtalk></dingtalk> to call in a page
The ultimate Solution
By encapsulating a component remote-js
implementation:
ExportDefault{ Components: { ' Remote-js ': { Render(CreateElement) { Return CreateElement(' Script ', { Attrs: { Type: ' Text/javascript ', Src: This.Src }});},Props:{ Src: { Type: String, required: true } ,},},},}
How to use:
<remote-js src="https://g.alicdn.com/dingding/dinglogin/0.0.2/ddLogin.js"></remote-js>
Transferred from: http://blog.csdn.net/sinat_17775997/article/details/55798611
Vue introduces a remote JS file