0

I am trying to insert a custom JavaScript (a sketcher) into a Vue component but no luck so far. When I try inserting javascript directly into a template like this:

<template>
    <div id="sketcher">
        var sketcher = new ChemDoodle.SketcherCanvas('sketcher', 400, 300);
    </div>
</template>

I get an error: "Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side-effects in your templates, such as , as they will not be parsed." My another attempt is to create a component in a different .js file and import it into a Vue component.

sketcher.js:

export default ({
  mounted(){    
   return new ChemDoodle.SketcherCanvas('sketcher', 400, 300)
  }
})

Home.vue

<template>
    <div id="sketcher">
       <app-sketcher></app-sketcher>
    </div>
</template>

<script>
import sketcher from './sketcher';    
export default {
    components: {
    "app-sketcher": sketcher
    }
}
</script>

but I get this error: "Failed to mount component: template or render function not defined". I would highly appreciate any suggestions on how to fix this or to propose other options of embedding javascript into a Vue component.

1 Answer 1

1

Your first error message is self-explanatory: you cannot put a <script> tag in the <template> of a single-file Vue component.

In your second example, you aren't defining a template for the app-sketcher component when you register it in your Home.vue file. Your sketcher.js file exports just the definition for a mounted hook and nothing else.


You can make a Sketcher.vue file:

<template>
  <div id="sketcher"></div>
</template>

<script>
export default {
  mounted() {    
    return new ChemDoodle.SketcherCanvas('sketcher', 400, 300)
  }
}
</script>

And then use import it and use it in your Home.vue file like so:

<template>
  <app-sketcher/>
</template>

<script>
import AppSketcher from './Sketcher';    
export default {
  components: { AppSketcher }
}
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your help but I get this error after implementing your suggestions: [Vue warn]: Error in mounted hook: "TypeError: c is undefined" found in ---> <AppSketcher> at src/components/Sketcher.vue <Home> at src/components/Home.vue <VApp> <App> at src/App.vue <Root>
You shouldn't be getting that error. Does your Sketcher.vue file look exactly like my example? If not, you probably have a syntax error somewhere in that file's mounted hook
I found my mistake and besides a missing '.vue' in import line, your code is working great. Thanks a lot.
The sketcher is displayed on the page but unfortunately it is mounted to the bottom of the page. After inspection I found that the sketcher was inserted below <script src="./dist/build.js"></script>. Any ideas why this can happen?

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.