简体   繁体   English

在 Gridsome 中使用 jQuery 和 Popper.js

[英]Use jQuery and Popper.js in Gridsome

I want to use a Bootstrap Material Design Library ( https://fezvrasta.github.io/bootstrap-material-design/ ) in my Gridsome project.我想在我的 Gridsome 项目中使用 Bootstrap 材料设计库( https://fezvrasta.github.io/bootstrap-material-design/ )。

Unfortunately jQuery and Popper.js don't work (jQuery/Popper.js not defined).不幸的是 jQuery 和 Popper.js 不起作用(jQuery/Popper.js 未定义)。 Using default Bootstrap 4 works like a charme.使用默认的 Bootstrap 4 就像一个魅力。

This is my main.js:这是我的 main.js:

import DefaultLayout from '~/layouts/Default.vue'
import "bootstrap-material-design"
import '~/assets/styles/app.scss'

export default function (Vue, { router, head, isClient }) {
  // Set default layout as a global component
  Vue.component('Layout', DefaultLayout);
}

Additionally importing Bootstrap and jQuery and Popper.js doesn't work.此外,导入 Bootstrap 和 jQuery 和 Popper.js 不起作用。

Of course I could reference it directly in my html-template, but I want to include it in my build-process...当然我可以直接在我的 html 模板中引用它,但我想将它包含在我的构建过程中......

Using the scss-files also works with @import "~bootstrap-material-design/scss/bootstrap-material-design";使用 scss 文件也适用于@import "~bootstrap-material-design/scss/bootstrap-material-design"; in my app.scss.在我的 app.scss 中。

Any ideas on how to import jquery and popper.js in my main.js?关于如何在我的 main.js 中导入 jquery 和 popper.js 的任何想法?

You should try to import Jquery and Popper.js by yourself.您应该尝试自己导入 Jquery 和 Popper.js。

First of all add this lines to your package.json file:首先将此行添加到您的 package.json 文件中:

"dependencies": {
    "jquery": "^3.1.1" //or whaterver versions you need
    "popper.js": "^1.15.0",
 },

and then import those modules with:然后使用以下命令导入这些模块:

npm install

Now you need to add few lines in you src/main.js file:现在您需要在 src/main.js 文件中添加几行:

window.Popper = require('popper.js').default;

This line will load popper.js and make it available to be executed with bootstrap.此行将加载 popper.js 并使其可用于引导程序执行。 Then you can require jquery:然后你可以要求 jquery:

require('jquery')

I still have some problems with importing js assets in Gridsome when, for example, I try to go with a theme I downloaded from the internet but I find that If you look at the docs in theme the best way probably is to make your own version of those files by importing the modules that you need using this way我在 Gridsome 中导入 js 资产时仍然遇到一些问题,例如,我尝试使用从互联网下载的主题 go 但我发现如果您查看主题中的文档,最好的方法可能是制作自己的版本通过使用这种方式导入您需要的模块来收集这些文件

cheers干杯

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

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