Skip to content

Commit cf0d61f

Browse files
authored
Migrate: Single page components (#118)
1 parent bdd72ab commit cf0d61f

File tree

7 files changed

+167
-83
lines changed

7 files changed

+167
-83
lines changed

src/.vuepress/components/community/team/vuer-profile.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ export default {
240240
color: #4682b4;
241241
242242
&::after {
243-
content: "\f06a";
244-
font-family: FontAwesome;
243+
content: '\f06a';
244+
font-family: 'Font Awesome 5 Free';
245245
font-size: 0.75em;
246246
vertical-align: super;
247247
margin-left: 4px;

src/.vuepress/config.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ const sidebar = {
5151
'/guide/composition-api-introduction'
5252
]
5353
},
54+
{
55+
title: 'Tooling',
56+
collapsable: false,
57+
children: [
58+
'/guide/single-file-component'
59+
]
60+
},
5461
{
5562
title: 'Scaling Up',
5663
collapsable: false,
@@ -138,8 +145,7 @@ module.exports = {
138145
ariaLabel: 'Documentation Menu',
139146
items: [
140147
{ text: 'Guide', link: '/guide/introduction' },
141-
{ text: 'Style Guide', link: '/style-guide/' },
142-
{ text: 'Tooling', link: '/tooling/' }
148+
{ text: 'Style Guide', link: '/style-guide/' }
143149
]
144150
},
145151
{ text: 'API Reference', link: '/api/application-config' },
Loading

src/.vuepress/public/images/sfc.png

111 KB
Loading

src/.vuepress/styles/index.styl

+89-78
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,99 @@
1-
.demo {
2-
font-family: sans-serif;
3-
border: 1px solid #eee;
4-
border-radius: 2px;
5-
padding: 20px 30px;
6-
margin-top: 1em;
7-
margin-bottom: 40px;
8-
user-select: none;
9-
overflow-x: auto;
10-
}
1+
.demo
2+
font-family sans-serif
3+
border 1px solid #eee
4+
border-radius 2px
5+
padding 20px 30px
6+
margin-top 1em
7+
margin-bottom 40px
8+
user-select none
9+
overflow-x auto
1110

12-
.custom-block {
13-
padding: 0.1rem 1.5rem;
14-
margin: 2em 0;
15-
border-left-width: 0.5rem;
16-
border-left-style: solid;
17-
background-color: #f8f8f8;
18-
position: relative;
19-
border-bottom-right-radius: 2px;
20-
border-top-right-radius: 2px;
21-
margin: 1rem 0;
11+
.custom-block
12+
padding 0.1rem 1.5rem
13+
margin 2em 0
14+
border-left-width 0.5rem
15+
border-left-style solid
16+
background-color #f8f8f8
17+
position relative
18+
border-bottom-right-radius 2px
19+
border-top-right-radius 2px
20+
margin 1rem 0
2221

23-
&::before {
24-
position: absolute;
25-
top: 14px;
26-
left: -14px;
27-
color: #fff;
28-
width: 20px;
29-
height: 20px;
30-
border-radius: 100%;
31-
text-align: center;
32-
line-height: 20px;
33-
font-weight: bold;
34-
font-family: 'Dosis', 'Source Sans Pro', 'Helvetica Neue', Arial, sans-serif;
35-
font-size: 14px;
36-
}
22+
&::before
23+
position absolute
24+
top 14px
25+
left -14px
26+
color #fff
27+
width 20px
28+
height 20px
29+
border-radius 100%
30+
text-align center
31+
line-height 20px
32+
font-weight bold
33+
font-family 'Dosis', 'Source Sans Pro', 'Helvetica Neue', Arial, sans-serif
34+
font-size 14px
3735

38-
.custom-block-title {
39-
font-weight: 600;
40-
margin-bottom: -0.4rem;
41-
}
36+
.custom-block-title
37+
font-weight 600
38+
margin-bottom -0.4rem
4239

43-
&.info {
44-
border-color: #007bff;
45-
}
40+
&.info
41+
border-color #007bff
4642

47-
&.tip {
48-
border-color: #42b983;
49-
}
43+
&.tip
44+
border-color #42b983
5045

51-
&.warning {
52-
border-color: #dcbc1e;
53-
background-color: #f8f8f8;
54-
}
46+
&.warning
47+
border-color #dcbc1e
48+
background-color #f8f8f8
5549

56-
&.danger {
57-
border-color: #f66;
58-
background-color: #f8f8f8;
50+
&.danger
51+
border-color #f66
52+
background-color #f8f8f8
5953

60-
&::before {
61-
content: '!';
62-
background-color: #f66;
63-
}
64-
}
54+
&::before
55+
content '!'
56+
background-color #f66
57+
58+
&.details
59+
display block
60+
position relative
61+
border-radius 2px
62+
margin 1.6em 0
63+
padding 1.6em
64+
background-color #eee
65+
66+
h4
67+
margin-top 0
6568

66-
&.details {
67-
display: block;
68-
position: relative;
69-
border-radius: 2px;
70-
margin: 1.6em 0;
71-
padding: 1.6em;
72-
background-color: #eee;
73-
h4 {
74-
margin-top: 0;
75-
}
7669
figure,
77-
p {
78-
&:last-child {
79-
margin-bottom: 0;
80-
padding-bottom: 0;
81-
}
82-
}
83-
summary {
84-
outline: none;
85-
cursor: pointer;
86-
}
87-
}
88-
}
70+
p
71+
&:last-child
72+
margin-bottom 0
73+
padding-bottom 0
74+
75+
summary
76+
outline none
77+
cursor pointer
78+
79+
.scrimba,
80+
.vueschool
81+
background-color #e7ecf3
82+
padding 1em 1.25em
83+
border-radius 2px
84+
color #486491
85+
position relative
86+
margin 24px 0
87+
88+
a
89+
color: #486491
90+
position relative
91+
padding-left 16px
92+
93+
&::before
94+
content "\f144"
95+
font-family 'Font Awesome 5 Free'
96+
font-size 2em
97+
display inline-block
98+
color #73abfe
99+
vertical-align middle

src/guide/single-file-component.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Single File Components
2+
3+
## Introduction
4+
5+
In many Vue projects, global components will be defined using `app.component()`, followed by `app.mount('#app')` to target a container element in the body of every page.
6+
7+
This can work very well for small to medium-sized projects, where JavaScript is only used to enhance certain views. In more complex projects however, or when your frontend is entirely driven by JavaScript, these disadvantages become apparent:
8+
9+
- **Global definitions** force unique names for every component
10+
- **String templates** lack syntax highlighting and require ugly slashes for multiline HTML
11+
- **No CSS support** means that while HTML and JavaScript are modularized into components, CSS is conspicuously left out
12+
- **No build step** restricts us to HTML and ES5 JavaScript, rather than preprocessors like Pug (formerly Jade) and Babel
13+
14+
All of these are solved by **single-file components** with a `.vue` extension, made possible with build tools such as Webpack or Browserify.
15+
16+
Here's an example of a file we'll call `Hello.vue`:
17+
18+
<a href="https://codepen.io/team/Vue/pen/3de13b5cd0133df4ecf307b6cf2c5f94" target="_blank" rel="noopener noreferrer"><img src="/images/sfc.png" width="403" alt="Single-file component example (click for code as text)" style="display: block; margin: 15px auto; max-width: 100%"></a>
19+
20+
Now we get:
21+
22+
- [Complete syntax highlighting](https://github.com/vuejs/awesome-vue#source-code-editing)
23+
- [CommonJS modules](https://webpack.js.org/concepts/modules/#what-is-a-webpack-module)
24+
- [Component-scoped CSS](https://vue-loader.vuejs.org/en/features/scoped-css.html)
25+
26+
As promised, we can also use preprocessors such as Pug, Babel (with ES2015 modules), and Stylus for cleaner and more feature-rich components.
27+
28+
<a href="https://codesandbox.io/s/vue-single-file-component-with-pre-processors-mr3ik?file=/src/App.vue" target="_blank" rel="noopener noreferrer"><img src="/images/sfc-with-preprocessors.png" width="563" alt="Single-file component with pre-processors example (click for code as text)" style="display: block; margin: 15px auto; max-width: 100%"></a>
29+
30+
These specific languages are only examples. You could as easily use TypeScript, SCSS, PostCSS, or whatever other preprocessors that help you be productive. If using Webpack with `vue-loader`, it also has first-class support for CSS Modules.
31+
32+
### What About Separation of Concerns?
33+
34+
One important thing to note is that **separation of concerns is not equal to separation of file types.** 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.
35+
36+
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:
37+
38+
``` html
39+
<!-- my-component.vue -->
40+
<template>
41+
<div>This will be pre-compiled</div>
42+
</template>
43+
<script src="./my-component.js"></script>
44+
<style src="./my-component.css"></style>
45+
```
46+
47+
## Getting Started
48+
49+
### Example Sandbox
50+
51+
If you want to dive right in and start playing with single-file components, check out [this simple todo app](https://codesandbox.io/s/vue-todo-list-app-with-single-file-component-vzkl3?file=/src/App.vue) on CodeSandbox.
52+
53+
### For Users New to Module Build Systems in JavaScript
54+
55+
With `.vue` components, we're entering the realm of advanced JavaScript applications. That means learning to use a few additional tools if you haven't already:
56+
57+
- **Node Package Manager (NPM)**: Read the [Getting Started guide](https://docs.npmjs.com/packages-and-modules/getting-packages-from-the-registry) section about how to get packages from the registry.
58+
59+
- **Modern JavaScript with ES2015/16**: Read through Babel's [Learn ES2015 guide](https://babeljs.io/docs/en/learn). You don't have to memorize every feature right now, but keep this page as a reference you can come back to.
60+
61+
After you've taken a day to dive into these resources, we recommend checking out [Vue CLI](https://cli.vuejs.org/). Follow the instructions and you should have a Vue project with `.vue` components, ES2015, webpack and hot-reloading in no time!
62+
63+
### For Advanced Users
64+
65+
The CLI takes care of most of the tooling configurations for you, but also allows fine-grained customization through its own [config options](https://cli.vuejs.org/config/).
66+
67+
In case you prefer setting up your own build setup from scratch, you will need to manually configure webpack with [vue-loader](https://vue-loader.vuejs.org). To learn more about webpack itself, check out [their official docs](https://webpack.js.org/configuration/) and [webpack learning academy](https://webpack.academy/p/the-core-concepts).

src/style-guide/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,7 @@ $color-priority-d: #3f536d;
18101810
margin-top: 0;
18111811

18121812
&::before {
1813-
font-family: FontAwesome;
1813+
font-family: 'Font Awesome 5 Free';
18141814
margin-right: .4em;
18151815
}
18161816
}

0 commit comments

Comments
 (0)