Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

javascript - variable still not defined even after adding external JS in VUE component

I am trying to import function marked from https://cdn.jsdelivr.net/npm/marked/marked.min.js. I have injected <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script> by mount(). But it always throws an error [Vue warn]: Error in render: "ReferenceError: marked is not defined". Why it is not defined even after the script has been added?

<script>
export default {
  name: 'Upload',
  data () {
    return {
      title: '',
      content: ''
    }
  },
  mounted () {
    let externalScript = document.createElement('script')
    externalScript.setAttribute('src', 'https://cdn.jsdelivr.net/npm/marked/marked.min.js')
    Plugin.async = true
    document.head.appendChild(externalScript)
  },
  computed: {
    markdown2html: function () {
      return marked(this.content)
    }
  }
}
</script>
question from:https://stackoverflow.com/questions/65878192/variable-still-not-defined-even-after-adding-external-js-in-vue-component

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Okay I'm not familiar with Vue, I use React, but normally I would be able to add the script link in the index.html under public folder. Then when everything's built it will be there.

Best option I think would be to install it via npm - npm i marked, then import marked from 'marked' - why not do it this way?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...