Vue introduces a remote JS file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.