panthro
panthro

Reputation: 24061

Conditionally add a JS script?

I'm trying to include an external script depending on a var:

<div v-if="shouldInclude">
    <script src="/my-script.js"></script>
</div>

Yet according to the network panel, the script is loaded when shouldInclude is both true and false.

Where am I going wrong?

Upvotes: 0

Views: 1021

Answers (1)

PatricNox
PatricNox

Reputation: 3918

You may need to use require instead, and place it inside a lifecycle hook.

created: function () {
    if (this.shouldInclude) {
      require('./my-script.js');
    }
}

Upvotes: 1

Related Questions