Hello, I am facing an issue while trying to import jQuery and THREEJS globally into my Vue.js cli project.
Whenever I attempt to import them into my .vue file (like home.vue
), I encounter the following error:
'import' and 'export' may only appear at the top level (5:4)
As a workaround, I tried importing them into main.js using
window.$ = window.jQuery = require('jquery')
, but then I get the error:
'$' is not defined
The same issue persists with THREEJS.
I have searched online for solutions, but most of the answers are related to Vue.js 2.
Here is the script in my Home.vue
file:
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(50, 1, 0.1, 1000);
var renderer = new THREE.WebGLRenderer({alpha: true});
renderer.setSize(200, 200);
$('.icon').append(renderer.domElement);
var geometry = new THREE.IcosahedronBufferGeometry();
var edges = new THREE.EdgesGeometry(geometry);
var line = new THREE.LineSegments(edges, new THREE.LineBasicMaterial({color: 0xffffff}));
scene.add(line);
camera.position.z = 2.5;
var animate = function () {
requestAnimationFrame(animate);
line.rotation.x += 0.0025;
line.rotation.y += 0.01;
renderer.render(scene, camera);
};
animate();
This is where I attempted to call THREE and jQuery globally:
import { createApp } from 'vue'
import App from './App.vue'
import Loading from './Loading.vue'
import router from './router'
window.$ = require('jquery')
window.JQuery = require('jquery')
window.THREE = require('three')
createApp(App).use(router).mount('#app')
createApp(Loading).mount('#loading')
Here is my package.json file:
{
"name": "portfolio-app",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"core-js": "^3.6.5",
"vue": "^3.0.0",
"vue-nprogress": "^0.2.0",
"vue-router": "^4.0.0-0"
},
"devDependencies": {
"@vue/cli-plugin-babel": "~4.5.0",
"@vue/cli-plugin-eslint": "~4.5.0",
"@vue/cli-plugin-router": "^4.5.9",
"@vue/cli-service": "~4.5.0",
"@vue/compiler-sfc": "^3.0.0",
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^7.0.0-0",
"node-sass": "^4.12.0",
"sass-loader": "^8.0.2"
}
}