What are some ways to implement src imports in Vue3?

Can you guide me on using a component in VUE3 with composition API and script setup pattern? For example, let's say I have a component named Modal. Here is how I plan to structure the folder:

  1. Modal.vue - this file will contain the Vue template and import the script and SCSS as shown below:

    
<script src="./index.js">
    <style lang="scss" scoped> @import './style.scss'; </style>

  2. index.js - where the JavaScript code resides. The issue arises when attempting to use the <script setup> tag here. Any insights on why?

  3. style.scss - containing the CSS styles.

Regarding the SPA:

<template>
  <div class="modals">
    <h1>Modals</h1>
  </div>
</template>

<script setup>
/*
  imports
*/

import { ref } from 'vue';
import Modal from '@/components/Modal.vue';
import ModalDark from '@/components/ModalDark.vue';

/*
  modals
*/

const showDarkModals = ref(false);
const showModal = ref(false);

const hideModal = () => (showModal.value = false);
const displayModal = () => showModal.value = true;
</script>

I envision something like this:

A folder named Modal with the following structure: https://i.sstatic.net/0L2dr.png

index.vue

<template>
   <div class="modals">
        <h1>Modals</h1>
   </div>
</template>
    
<script src="./index.js" setup></script>
    
<style> @import './style.css'; </style>

index.js

/*
  imports
*/
<script>

import { ref } from 'vue';
import Modal from '@/components/Modal.vue';
import ModalDark from '@/components/ModalDark.vue';

/*
  modals
*/

const showDarkModals = ref(false);
const showModal = ref(false);

const hideModal = () => (showModal.value = false);
const displayModal = () => showModal.value = true;
</script>

style.css

.modals {
  background: red;
}

OUTPUT:

https://i.sstatic.net/SUeod.png

Answer №1

Ensure that you do not enclose the content of the .js file within script tags, as you are not working in an HTML document. By including the file in <script setup />, you are already indicating to use JavaScript code, so there is no need for additional script tags.

This is similar to handling a CSS file: Simply remove the initial and closing HTML tags (<script> and </script>) from the .js file, and everything should function correctly.

If you encounter issues, the error message might indicate that script setup cannot be paired with external file imports, requiring you to directly integrate the JavaScript code into your .vue component file.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process for choosing a category in the freebase search widget?

For my current project, I have been utilizing the Freebase Search Widget. It allows me to select words from a suggestion list in my input box. However, I am curious about how to also obtain the category in another text box. Here is an example that demonst ...

Having trouble assigning more than one custom named property in TypeScript for MUI v5 Palette

I am currently setting up multiple custom attributes to make future updates easier. Nonetheless, I'm encountering a challenge with implementing more than one custom property in MUI v5. TypeScript Error TS2717: Subsequent property declarations must hav ...

Vue.js - The dissonance between data model and displayed output

Below is a simplified example of my issue: <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script> <script src="https://unpkg.com/vue/dist/vue.js"></script> ...

The gradual vanishing of principles

Below you will find text fields that I am populating with values that are later utilized in a JavaScript function. These fields are all contained within a form. The issue I am encountering is that when I submit the form, I initially get the correct value ...

Finding a JSON file within a subdirectory

I am trying to access a json file from the parent directory in a specific file setup: - files - commands - admin - ban.js <-- where I need the json data - command_info.json (Yes, this is for a discord.js bot) Within my ban.js file, I hav ...

Create object keys without relying on any ORM or database management systems like mongoose or mongodb

Exploring a Mongoose Object: recipe = { "ingredients": [ { "_id": "5fc8b729c47c6f38b472078a", "ingredient": { "unit": "ml", "name" ...

Easily accessible jQuery tabs with the option to directly link to a specific tab

I currently have tabs set up on my webpage. The code for these tabs is as follows: $('ul#tabs li a').click(function(){ var id = $(this).attr('id'); var counter = 1; $("ul#tabs a.current").removeClass('cur ...

Using Vue.js to handle asynchronous functions with undefined variables

My Vue.js application is facing a problem where an async function is passing a variable as undefined even though it's properly defined before the function call. The async function FETCH_DATA in my Vue.js application is defined like this: async [FETCH ...

Exploring ways to utilize removeEventListener in React Native

Can someone assist me with converting this code to React? I am new to using React and struggling with the implementation. Thank you in advance! <progress id="bar" value="0" max="110"></progress> <button onClick={i ...

Having trouble getting the Vue Sidebar Transition to work properly with tailwind CSS

I am having trouble making the sidebar function properly in relation to this link Opening works fine, but closing does not work for both overlay and sliding back. <div class="fixed inset-0 flex z-40 lg:hidden" role="dialog" aria-mod ...

Incorporating a basic search feature into Ajax

At the moment, only results appear when you modify the query. I want to modify this to allow user input. I have set up the fields, but I need help adjusting my ajax code to accept the new search criteria which is crucial for functionality. This is what m ...

Server Sent Events not being received by client from Express.js server

My Next.js (React) client is set up to receive Server-Sent Events from my Node.js/Express.js server, but it seems like it's not receiving any messages for some unknown reason. While the open and error events of EventSource are functioning correctly, ...

limit mongoose search results to a specific year

Would it be possible to add an option for the api user to filter the wine query by year? However, if no year is specified, mongoose should not return an empty array. The same applies to the price property. For example, http://localhost:1234/api/wine?year= ...

Angular animation triggered when a specific condition is satisfied

I am working on an animation within my Angular application @Component({ selector: 'app-portfolio', templateUrl: 'portfolio.page.html', styleUrls: ['portfolio.page.scss'], animations: [ trigger('slideInOut&apo ...

Expand the <div> by clicking on it, then hover away to return it to its normal size

One interesting feature I have on my website is a <div> that expands when clicked, and returns to normal size with another click. However, I am looking for something a bit different... What I want is for the <div> (with the class name .topHead ...

Using Selenium Webdriver with Node.js to Crop Images

Currently, I am using selenium-webdriver in combination with nodejs to extract information from a specific webpage. The page contains a captcha element from which I need to retrieve the image. Unfortunately, all the code snippets and solutions I came acros ...

How can I stretch a background image using jquery to cover the entire document instead of just the window

I have implemented a plugin called https://github.com/srobbin/jquery-backstretch to effectively stretch the background image. The problem arises when the content of the page is long enough to create a scrollbar. In this case, while scrolling, the backgrou ...

AngularStrap's bs-modal is failing to display content

I have a question, here is a Plunker with the code: http://plnkr.co/edit/SNFy2XcOBefUavG1QCqD?p=preview <button class="btn btn-default" data-template="customer.tpl.html" bs-modal="modal">New Customer </button> < ...

Is it possible to utilize Angular validation directives programmatically within a personalized directive?

In my exploration of HTML inputs, I have noticed a recurring pattern specifically for phone numbers: <input type="text" ng-model="CellPhoneNumber" required ng-pattern="/^[0-9]+$/" ng-minlength="10" /> I am interested in developing a unique directiv ...

The function form.parse() in node.js is always overlooked

I'm having an issue where form.parse() is never called. When I delete bodyparser, my session variable throws an error. How can I resolve this and make it work? logcat The write(string, encoding, offset, length) method is deprecated. Use write(st ...