I’m asked to add a new html page to a vuejs project. This page is already fully developed and is based on jquery and some twitter-bootstrap features.
I’ve created a new component.
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');
});
}
But this obviously does not work because collapse.js
cannot access jQuery
and I get this error
Uncaught ReferenceError: jQuery is not defined
How do I fix this problem?
Given that I don’t want (if possible) to add bootstrap and jquery globally to my project because this will surely breaks here and there in my other components?
2
Answers
I figured it out,
require
works while import does not.console.log($)
if they’ve built it on jQuery chances are it’s already there.