You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 8, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: src/guide/single-file-component.md
+30-31Lines changed: 30 additions & 31 deletions
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,7 @@
2
2
3
3
## 介绍
4
4
5
-
<!-- TODO: translation -->
6
-
Vue Single File Components (aka `*.vue` files, abbreviated as **SFC**) is a special file format that allows us to encapsulate the template, logic, **and** styling of a Vue component in a single file. Here's an example SFC:
As we can see, Vue SFC is a natural extension of the classic trio of HTML, CSS and JavaScript. Each`*.vue`file consists of three types of top-level language blocks: `<template>`, `<script>`, and`<style>`:
Check out more details in the [SFC Syntax Specification](/api/sfc-spec).
36
+
查阅 [SFC 语法规范](/api/sfc-spec) 查看更多细节。
38
37
39
-
## How It Works
38
+
## 工作原理
40
39
41
-
Vue SFC is a framework-specific file format and must be pre-compiled by [@vue/compiler-sfc](https://github.com/vuejs/vue-next/tree/master/packages/compiler-sfc)into standard JavaScript and CSS. A compiled SFC is a standard JavaScript (ES) module - which means with proper build setup you can import an SFC like a module:
`<style>`tags inside SFCs are typically injected as native `<style>`tags during development to support hot updates. For production they can be extracted and merged into a single CSS file.
In actual projects, we typically integrate the SFC compiler with a build tool such as [Vite](https://vitejs.dev/)or[Vue CLI](http://cli.vuejs.org/) (which is based on [webpack](https://webpack.js.org/)), and Vue provides official scaffolding tools to get you started with SFCs as fast as possible. Check out more details in the [SFC Tooling](/api/sfc-tooling)section.
SFC is a defining feature of Vue as a framework, and is the recommended approach for using Vue in the following scenarios:
70
+
SFC 是 Vue 作为框架的定义特性,也是在以下场景中使用 Vue 的推荐方法:
72
71
73
-
-Single-Page Applications (SPA)
74
-
-Static Site Generation (SSG)
75
-
-Any non-trivial frontends where a build step can be justified for better development experience (DX).
72
+
-单页应用(SPA)
73
+
-静态站点生成(SSG)
74
+
-任何重要的前端,其中构建步骤可以得到更好的开发体验(DX)。
76
75
77
-
That said, we do realize there are scenarios where SFCs can feel like overkill. This is why Vue can still be used via plain JavaScript without a build step. If you are just looking for enhancing largely static HTML with light interactions, you can also check out [petite-vue](https://github.com/vuejs/petite-vue), a 5kb subset of Vue optimized for progressive enhancement.
Some users coming from a traditional web development background may have the concern that SFCs are mixing different concerns in the same place - which HTML/CSS/JS were supposed to separate!
80
+
一些来自传统 Web 开发背景的用户可能会担心 SFC 在同一个地方混合了不同的关注点——HTML/CSS/JS 应该分开!
82
81
83
-
To answer this question, it is important for us to agree that **separation of concerns is not equal to separation of file types.** The ultimate goal of engineering principles is to improve maintainability of codebases. Separation of concerns, when applied dogmatically as separation of file types, does not help us reach that goal in the context of increasingly complex frontend applications.
In modern UI development, we have found that instead of dividing the codebase into three huge layers that interweave with one another, it makes much more sense to divide them into loosely-coupled components and compose them. Inside a component, its template, logic, and styles are inherently coupled, and collocating them actually makes the component more cohesive and maintainable.
Note even if you don't like the idea of Single-File Components, you can still leverage its hot-reloading and pre-compilation features by separating your JavaScript and CSS into separate files using [Src Imports](/api/sfc-spec.html#src-imports).
0 commit comments