1

I'm trying to create SPA\PWA using Vue Js.

I want to import a file of functions to public folder from src folder.

enter image description here

so i do this:

index.html file in public folder:

enter image description here

in the first time i get this err: Uncaught SyntaxError: Cannot use import statement outside a module then I added type="module" but now i get this err: GET http://localhost:8080/src/scripts/sc.js net::ERR_ABORTED 404 (Not Found)

how can i fix that?

11
  • Why do you want to do this in the first place? What problem are you trying to solve this way? Commented Mar 27, 2022 at 17:05
  • @ThomasJunk In the sc.js file there is a function ```getOS`` that determines the os of the device` I want to use to accept only Android and iOS users to use the site. Commented Mar 27, 2022 at 17:15
  • Okay. My bad. I asked imprecise. Why do you want to "import" the package within the context of the index.html in contrast to just import it in your main.js as any other function? What makes you think you have to solve your problem this way and not in another way? Maybe the problems you are facing stem from choosing the wrong approach? Commented Mar 27, 2022 at 17:26
  • If you want to use imports in your script, you should build a js bundle with webpack/babel or similar. Your frontend code can't known about your source code files tree. Commented Mar 27, 2022 at 17:26
  • Related SO question: stackoverflow.com/questions/66867833/… Commented Mar 27, 2022 at 17:27

2 Answers 2

1

Just import your script.js in main.js or which is entry level file you need to import inside it.

Sign up to request clarification or add additional context in comments.

Comments

0

if i understand you want only ios and android view your app you can use an package like vue-mobile-detection or device-detector-js

npm i vue-mobile-detection

Plugin installation

import VueMobileDetection from "vue-mobile-detection";
import App from "./App.vue";

const app = createApp(App);
app.use(VueMobileDetection);

/* After the install you can use this.$isMobile() in all your vue components */

Using the plugin

<template>
  <div id="app">
    <!-- Use in template -->
    <div v-if="$isMobile()">MOBILE</div>
    <div v-else>DESKTOP OR TABLET (Denied Access)</div>
  </div>
</template>

<script>
export default {
  created() {
    // Use in js
    console.log(this.$isMobile());
  }
};
</script>

also you can use route or your own isMobile function please refer to this thread

Display different Vuejs components for mobile browsers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.