简体   繁体   English

Vue3:是否可以将包含组件的 HTML 文件导入 SFC

[英]Vue3: is it possible to import a HTML file with components in it to a SFC

I have a Form-Component (SFC) that should import a HTML file with Vue components in it.我有一个表单组件 (SFC),它应该导入一个 HTML 文件,其中包含 Vue 组件。

Form-Component表单组件

<template src="../../../views/form-settings.html"></template>

<script setup>
import Button from "./../ui/button/Button.vue";
</script>

HTML File (form-settings.html) HTML 文件 (form-settings.html)

<div>
     <Button>test button</Button>
</div>

package.json package.json

"devDependencies": {
    "@vue/compiler-sfc": "^3.2.31",
    "laravel-mix": "^6.0.6",
    "vue-loader": "^17.0.0"
},
"dependencies": {
    "vue": "^3.2.31",
},

The HTML file gets loaded correctly but the Button-Component in it not. HTML 文件被正确加载,但其中的 Button-Component 没有。 So I get the error: [Vue warn]: Failed to resolve component: Button If this is a native custom element, make sure to exclude it from component resolution via compilerOptions.isCustomElement.所以我收到错误: [Vue warn]: Failed to resolve component: Button 如果这是本机自定义元素,请确保通过 compilerOptions.isCustomElement 将其从组件解析中排除。

Yes it is possible.是的,有可能。

You can even import the script and style section:您甚至可以导入脚本和样式部分:

<template src="./template.html"></template>
<style src="./style.css"></style>
<script src="./script.js"></script>

Read it in the docs .在文档中阅读它


I don't know why your code doesn't work, I tried to create a similar demo and it works fine.我不知道为什么您的代码不起作用,我尝试创建一个类似的演示并且它运行良好。 See it here on Stackblitz 在 Stackblitz 上查看

I have now solved the problem myself.我现在已经自己解决了这个问题。

I had to import the components I want to use in the HTML file in app.js and then initialize them with Vue.我不得不在 app.js 的 HTML 文件中导入我想使用的组件,然后用 Vue 初始化它们。

import BaseButton from "./components/ui/BaseButton.vue";
app.component("BaseButton", BaseButton);

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

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