简体   繁体   English

如何将npm包中的JS文件包含到Nuxt.js中的单独页面

[英]How to include JS file from npm package to separate page in Nuxt.js

I have several npm packages with client-side scripts I want to include to separate pages of my Nuxt.js project. 我有几个带有客户端脚本的npm软件包,我想将它们包括在我的Nuxt.js项目的单独页面中。 I have tried to do that with: 我试图做到这一点:

<script>
export default {
  head: {
    script: [
      { src: "gojs/release/go.js", type: "text/javascript" }
    ]
  },

  // ...
}
</script> 

but received 404 (Not Found) error. 但收到404 (Not Found)错误。 What is correct way to do that? 正确的方法是什么?

Normally, import -ing the module (eg, with import go from 'gojs' ) and using it in your code are all you need to do for the build to include that dependency. 通常, import go from 'gojs'将模块import (例如, import go from 'gojs' )并在代码中使用它,以使构建包含该依赖项。 No need to add headers to load the NPM module. 无需添加标题即可加载NPM模块。

Example in MyFoo.vue : MyFoo.vue中的MyFoo.vue

<script>
import go from 'gojs'

export default {
  mounted() {
    const $ = go.GraphObject.make;
    const myDiagram =
      $(go.Diagram, "myDiagramDiv",
        {
          "undoManager.isEnabled": true
        });

    ...
  }
}
</script>

demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM