简体   繁体   English

vue.js 2 - 将单个文件组件的 html 片段提取到独立文件中

[英]vue.js 2 - extract html snipped of single file component into standalone file

I'm wondering if there is a way to split a Single File Components file into multiple smaller ones.我想知道是否有办法将单个文件组件文件拆分为多个较小的文件。

What am I talking about?我在说什么? Let's look at this:让我们看看这个:

<template>
  ... 
</template>

<script>
  export default {
    name: 'my-component',
  };
</script>

<style scoped>
  ...
</style>

That is the way the typical vue.js single file component is built up.这就是典型的 vue.js 单文件组件的构建方式。 As you can see, there is a html -section (content of <template> tag), a javascript -section (content of <script> tag) and a css -section (content of style tag).如您所见,有一个html -section( <template>标签的内容)、一个javascript -section( <script>标签的内容)和一个css -section( style标签的内容)。 I was wondering if it is possible to split all of those sections into single files.我想知道是否可以将所有这些部分拆分为单个文件。 Let's call them my-component.css , my-component.es6 and my-component.html - all 'belonging' to my-component.vue .我们称它们为my-component.cssmy-component.es6my-component.html - 都“属于” my-component.vue

I managed to extract the javascript and the css styling into standalone files like so:我设法将javascript和css样式提取到独立文件中,如下所示:

<template>
  ...
</template>

<script src="./my-component.es6"></script>

<style scoped>
  @import './my-component.css';
</style>

This works just fine.这工作得很好。 Now I'm just left with the html -section.现在我只剩下html部分了。 Is there a way to extract this one also?有没有办法也提取这个?

Why?为什么?

I like to have a clear code separation and this is the only way to keep the 'code-mixing' to a minimum within the my-component.vue file.我喜欢清晰的代码分离,这是在my-component.vue文件中将“代码混合”保持在最低限度的唯一方法。

The argument here is what is code separation.这里的论点是什么是代码分离。 The author of Vue argues that having HTML, CSS and JS in one .vue file is separating the components code to their own place and keeping their related code together and is not relevant to 'separation of concerns'. Vue 的作者认为,在一个.vue文件中包含 HTML、CSS 和 JS 是将组件代码分离到它们自己的位置并将它们的相关代码保持在一起,并且与“关注点分离”无关。 It's talked about briefly in the docs here:在这里的文档中简要讨论了它:

https://v2.vuejs.org/v2/guide/single-file-components.html#What-About-Separation-of-Concerns . https://v2.vuejs.org/v2/guide/single-file-components.html#What-About-Separation-of-Concerns

So you have 3 different choices:所以你有3种不同的选择:

  1. Use .vue files as they are intended.按预期使用.vue文件。
  2. Use the webpack plugin (mentioned in other comment).使用 webpack 插件(在其他评论中提到)。
  3. Build your components without using a .vue file however you will lose the ability to hot reload during development and you will have to use the html-loader plugin.在不使用.vue文件的情况下构建您的组件,但是您将失去在开发过程中热重载的能力,您将不得不使用html-loader插件。

HTML: HTML:

<div id="example">
  <my-component></my-component>
</div>

JS: JS:

// register
Vue.component('my-component', {
  template: require('my-component.html');
})

// create a root instance
new Vue({
  el: '#example'
})

Opinion: Just use the .vue file as shown in the docs.意见:只需使用.vue文件,如文档中所示。 It will be easier to maintain, easier to find your code and you won't have to wrestle with webpack.它会更容易维护,更容易找到你的代码,而且你不必与 webpack 搏斗。 Coming from an Angular world I found this a bit weird but I've learned to love it.来自 Angular 世界,我觉得这有点奇怪,但我已经学会了喜欢它。

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

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