【发布时间】:2018-07-18 04:08:31
【问题描述】:
我想知道是否有办法将Single File Components 文件拆分为多个较小的文件。
我在说什么? 让我们看看这个:
<template>
...
</template>
<script>
export default {
name: 'my-component',
};
</script>
<style scoped>
...
</style>
这就是典型的 vue.js 单文件组件的构建方式。如您所见,有一个html-section(<template> 标签的内容)、一个javascript-section(<script> 标签的内容)和一个css-section(style 标签的内容) )。我想知道是否可以将所有这些部分拆分为单个文件。让我们称它们为 my-component.css、my-component.es6 和 my-component.html - 都“属于”my-component.vue。
我设法将 javascript 和 css 样式提取到独立文件中,如下所示:
<template>
...
</template>
<script src="./my-component.es6"></script>
<style scoped>
@import './my-component.css';
</style>
这很好用。现在我只剩下html-部分了。 有没有办法也提取这个?
为什么?
我喜欢清晰的代码分离,这是在my-component.vue 文件中将“代码混合”保持在最低限度的唯一方法。
【问题讨论】:
-
@Veehmot 我一直在寻找相关问题,但找不到任何问题。谢谢,我去看看
-
有一个 webpack 插件。可能有助于减少自己处理这一切的工作。 github.com/pksunkara/vue-builder-webpack-plugin
-
就我个人而言,我喜欢相反的方式——把所有东西都放在一个地方,但是是的,我也能理解你的愿望。那么为什么要使用单个文件组件 - 为什么不只是去“老派”并单独编写它(然后在需要时使用 Webpack 或其他东西打包)?
-
@nettutvikler 因为我喜欢单文件组件的概念。并作为补充;我对 webpack 的经验非常有限。 ..
标签: html vue.js vuejs2 vue-component code-separation