简体   繁体   English

如何在 Vue3 中使用 src 导入?

[英]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.我想问你如何在 VUE3 中使用组合 API -> 脚本设置模式的组件,例如。 -> I have a component: Modal. -> 我有一个组件:Modal。 So I'm gonna create a folder Modal which has:所以我要创建一个文件夹 Modal ,它有:

  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> Modal.vue -> vue 模板在哪里,并像这样导入脚本和 scss: <script src="./index.js"> <style lang="scss" scoped> @import './style.scss'; </style> <style lang="scss" scoped> @import './style.scss'; </style>

  2. index.js -> where is the js code. index.js -> js 代码在哪里。 Here is the problem!!问题来了!! If I'm trying to use the <script setup> the code here isn't working.如果我尝试使用<script setup>此处的代码不起作用。 Any idea why?知道为什么吗?

  3. style.scss -> where is the css style. style.scss -> CSS 样式在哪里。

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:一个名为 Modal 的文件夹,其结构如下: 在此处输入图像描述

index.vue索引.vue

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

index.js 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样式.css

.modals {
  background: red;
}

OUTPUT:输出:

在此处输入图像描述

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.您不能用script标签包装.js文件的内容,因为您没有在 HTML 文件中编写此代码。 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.您已经通过在<script setup />中导入文件来指定使用 javascript 代码,因此编译器知道.js文件的内容包含 JavaScript 代码。

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.它类似于CSS文件:只需从.js文件的开头和结尾删除 HTML 标记( <script></script> ),就可以了。

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.如果这不起作用,则错误消息表明script setup不能与外部文件导入一起使用,因此您可能被迫将 JavaScript 代码直接写入您的.vue -file。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM