0

For example,

// Example external script
<script src="https://test.abcd.com/js/test.1.0.0.js" />

<script>
  export default {
      name: 'HelloWorld',
      props: {
          msg: String,
      },
      methods: {
          myFunction() {

              // This is the example function of the example external script
              exampleExternalFunction();
      },
   },
};
</script>

In this way, I want to add an external script in vue.js and use the functions contained in the external script, but I get an error.

Is there any other way to do this?

3
  • global scripts should generally be placed in your index.html page, which contains the head and body Commented May 22, 2020 at 10:08
  • What kind of error do you get? Commented May 22, 2020 at 14:29
  • To add a script, I figured out how to add it using mounted(). Then, when I try to use the function contained in the script, an error called an undefined function occurs. This error occurs not only when using a function by adding a script to the vue component, but also when adding a script to index.html and trying to use the function in the vue component. Commented May 24, 2020 at 4:15

1 Answer 1

1

I figured out solution. I used ESLint, but ESLint was blocking an undefined function. So I added ESLint rule called no-undef. Of course,I added the external script before that.

<script>
export default {
    name: 'HelloWorld',
    props: {
        msg: String,
    },
    created() {
        const examExternalScript = document.createElement('script');
        examExternalScript.setAttribute('src', 'https://test.abcd.com/js/test.1.0.0.js');
        document.head.appendChild(examExternalScript);
    },
    methods: {
        myFunction() {

          // This is the example function of the example external script
          // eslint-disable-next-line no-undef
          exampleExternalFunction();
        },
    },
};
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.