Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

239
Visualizações
How can I use the src imports in Vue3?

I wanna ask you how can I use a component in VUE3 with composition API -> script setup pattern, for eg. -> I have a component: Modal. So I'm gonna create a folder Modal which has:

  1. Modal.vue -> where is the vue template and import the script and scss like this: 
<script src="./index.js"> 
 <style lang="scss" scoped>   @import './style.scss'; </style>


  2. index.js -> where is the js code. Here is the problem!! If I'm trying to use the <script setup> the code here isn't working. Any idea why?


  3. style.scss -> where is the css style.



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>

And I want something like this:

A folder called Modal and which have the following structure: enter image description here

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:

enter image description here

  • reference: https://vuejs.org/api/sfc-spec.html#src-imports
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You must just not wrap the content of the .js-file with script tags, since you are not writing this code in an HTML file. You do already specify to use javascript code by importing the file in <script setup />, so the compiler knows that the content of the .js-file contains JavaScript code.

It is similar to the CSS-file: Just delete the HTML-tags (<script> and </script>) from the beginning and the end of the .js-file, and you should be fine.

If this does not work, the error message says that script setup cannot be used with external file imports, so you might be forced to write the JavaScript code directly into your .vue-component-file.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda