简体   繁体   English

如果我们拥有.js中的所有详细信息,.vue .js文件之间有什么区别?

[英]What's the difference between .vue .js files if we have all the details in .js?

I have read the vue docs. 我已经阅读了vue文档。 My global understanding of vue structure is based on statements: 我对vue结构的全局理解基于以下陈述:

  • index.html has container that points to a .vue file index.html具有指向.vue文件的容器

  • .vue has 3 separate things: with ONLY 1 div , (logic) , (css) .vue具有3个独立的元素:仅使用1 div,(logic),(css)

  • vue should have data(), methods(), computedproperties() etc vue应该具有data(),methods(),computedproperties()等

But .js file can have same code as .vue 但是.js文件可以与.vue具有相同的代码

So what's the difference between them, or am I missing something? 那么他们之间有什么区别,或者我错过了什么?

*.vue files are there during build time only. *.vue文件仅在构建时存在。 Vue files can contain <template /> , <script /> and <style /> tags. Vue文件可以包含<template /><script /><style />标记。 Webpack needs to know how to bundle *.vue imports. Webpack需要知道如何捆绑*.vue导入。 So we have vue-loader . 所以我们有vue-loader During build time, using vue-loader, your JavaScript in the Vue file is transpiled, the template is compiled to render functions, and styles are extracted. 在构建期间,使用vue-loader,将对Vue文件中的JavaScript进行编译,将模板编译为呈现函数,然后提取样式。 Just .js files won't be able to contain all <template /> , <script /> and <style /> tags together. 只是.js文件将无法一起包含所有<template /><script /><style />标记。

I personally prefer to use *.vue files with all these tags in there because that forces me to keep my components small and focussed. 我个人更喜欢将*.vue文件与所有这些标签一起使用,因为这迫使我保持​​组件的小巧和专注。 If the *.vue file gets too long, then it's usually an indication that my component is doing too much and I break it down into smaller components. 如果*.vue文件太长,则通常表明我的组件做得太多,我将其分解为较小的组件。 And, a small focused component with all its logic, template and style in one file is very convenient to work with. 而且,一个集中的小型组件及其所有逻辑,模板和样式都在一个文件中,使用起来非常方便。 Now and in the future. 现在和将来。

Using a .vue file allows you to include your template, js, and css in the same file. 使用.vue文件可让您将模板,js和css包含在同一文件中。

They will also allow for better performance if you want to precompile your templates, and you can use scoped css. 如果要预编译模板,它们还可以提供更好的性能,并且可以使用作用域的CSS。

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

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