【发布时间】:2018-01-24 14:31:48
【问题描述】:
我被要求向vuejs 项目添加一个新的html 页面。此页面已经完全开发并基于jquery 和一些twitter-bootstrap 功能。
我创建了一个新组件。
newPage.vue
<template>
<div id="my-new-page">
...
</div>
</template>
<style src="path/to/_bootstrap.scss" lang="scss" scoped></style>
<style src="path/to/font-awesome.scss" lang="scss" scoped></style>
<style src="path/to/animate.css" scoped></style>
<style src="path/to/custom/css.css" scoped></style>
<script src="path/to/custom/js.js"></script>
js.js
import jQuery from 'jquery';
// Same error even with window.$ = window.jQuery = jQuery;
import collapse from 'path/to/bootstrap/feature/collapse.js";
export default {
created() {
runjQuery(jQuery);
},
};
function runjQuery($) {
// here is how I thought integrate the jquery script of the new html page
$(function () {
....
$('#navbar').collapse('hide');
});
}
但这显然不起作用,因为collapse.js 无法访问jQuery,我收到此错误
Uncaught ReferenceError: jQuery is not defined
我该如何解决这个问题?
鉴于我不希望(如果可能)将 bootstrap 和 jquery 全局添加到我的项目中,因为这肯定会在我的其他组件中到处中断?
【问题讨论】:
-
你看过vue-strap吗? wffranco.github.io/vue-strap
-
@enriqg9 没有我需要的所有功能,例如折叠
-
我怀疑您需要
import的括号形式。见stackoverflow.com/a/43546877/392102 -
@RoyJ 注入jquery没问题,问题是注入依赖jquery的bootsrap时
标签: vuejs jquery twitter-bootstrap javascript jquery twitter-bootstrap vue.js