【问题标题】:Script inclusion in index.html on Vue.js application gives 404Vue.js 应用程序中 index.html 中的脚本包含 404
【发布时间】:2017-03-28 15:37:34
【问题描述】:

我的应用程序是通过运行以下命令生成的:

vue init webpack

在向导的第四个问题上回答“Y”,模板应用程序只安装了vue-router

我想包含来自node_modules 文件夹的 jquery:

npm install --save jquery

然后我添加这个

<head>
    <script src="node_modules/jquery/dist/jquery.js"></script>
</head>

index.html 页面上。

下一步:

npm run dev

应用程序正常启动,但我在 jquery.js 上得到 404。

除了index.html 中的那一行之外,我从未在整个应用程序中碰过任何东西。

我们在更大的应用程序中遇到了同样的问题,但我们不知道是否可以在更小的应用程序上重现......但我们设法做到了。

【问题讨论】:

    标签: javascript jquery html vue.js


    【解决方案1】:

    你必须在你的组件中导入它。 webpack 管理依赖。

    如果您使用过vue init webpack,那么您也在使用 vue-loader,因此要将 jquery 添加到 Hallo 组件中,您可以这样做

    首先你已经完成了npm install --save jquery

    然后打开文件 App.vue 并在该部分添加导入:

    <script>
    import Hello from './components/Hello'
    import jquery from 'jquery'
    
    export default {
      name: 'app',
      components: {
        Hello
      }
    }
    </script>
    

    不确定您在寻找什么,但这是添加 3rd 方库的方法

    【讨论】:

      猜你喜欢
      • 2019-01-26
      • 1970-01-01
      • 2014-09-12
      • 2018-03-28
      • 1970-01-01
      • 2019-04-24
      • 2021-09-17
      • 2019-07-30
      • 2020-09-12
      相关资源
      最近更新 更多