red
+ + + +``` + +### Usage with Composition API + +The injected classes can be accessed in `setup()` and ` + + +``` + +The syntax works with [` + + +hello
+ + + +``` + +The actual value will be compiled into a hashed CSS custom property, so the CSS is still static. The custom property will be applied to the component's root element via inline styles and reactively updated if the source value changes. diff --git a/src/api/sfc-tooling.md b/src/api/sfc-tooling.md new file mode 100644 index 0000000000..f38bc59292 --- /dev/null +++ b/src/api/sfc-tooling.md @@ -0,0 +1,94 @@ +# SFC Tooling + +## Online Playgrounds + +You don't need to install anything on your machine to try out Vue SFCs - there are many online playgrounds that allow you to do so right in the browser: + +- [Vue SFC Playground](https://sfc.vuejs.org) (official, deployed from latest commit) +- [VueUse Playground](https://play.vueuse.org) +- [Vue on CodeSandbox](https://codesandbox.io/s/vue-3) +- [Vue on Repl.it](https://replit.com/@templates/VueJS-with-Vite) +- [Vue on Codepen](https://codepen.io/pen/editor/vue) +- [Vue on StackBlitz](https://stackblitz.com/fork/vue) + +It is also recommended to use these online playgrounds to provide reproductions when reporting bugs. + +## Project Scaffolding + +### Vite + +[Vite](https://vitejs.dev/) is a lightweight and fast build tool with first-class Vue SFC support. It is created by Evan You, who is also the author of Vue itself! To get started with Vite + Vue, simply run: + +```sh +npm init vite@latest +``` + +Then select the Vue template and follow the instructions. + +- To learn more about Vite, check out the [Vite docs](https://vitejs.dev/guide/). +- To configure Vue-specific behavior in a Vite project, for example passing options to the Vue compiler, check out the docs for [@vitejs/plugin-vue](https://github.com/vitejs/vite/tree/main/packages/plugin-vue#readme). + +The [SFC Playground](https://sfc.vuejs.org/) also supports downloading the files as a Vite project. + +### Vue CLI + +[Vue CLI](https://cli.vuejs.org/) is the official webpack-based build tool for Vue projects. To get started with Vue CLI: + +```sh +npm install -g @vue/cli +vue create hello-vue +``` + +- To learn more about Vue CLI, check out [Vue CLI docs](https://cli.vuejs.org/guide/installation.html). + +### Vite or Vue CLI? + +We recommend starting new projects with Vite as it offers significantly better development experience in terms of dev server startup and HMR update performance ([details](https://vitejs.dev/guide/why.html)). Only go with Vue CLI if you rely on specific webpack features (e.g. Module Federation). + +If you are a [Rollup](https://rollupjs.org/) user, you can safely adopt Vite as it uses Rollup for production builds and supports a Rollup-compatible plugin system. [Even Rollup's maintainer recommends Vite as THE web development wrapper for Rollup](https://twitter.com/lukastaegert/status/1412119729431584774). + +## IDE Support + +The recommended IDE setup is [VSCode](https://code.visualstudio.com/) + the [Volar](https://github.com/johnsoncodehk/volar) extension. Volar provides syntax highlighting and advanced IntelliSense for template expressions, component props and even slots validation. We strongly recommend this setup if you want to get the best possible experience with Vue SFCs, especially if you are also using TypeScript. + +[WebStorm](https://www.jetbrains.com/webstorm/) also provides decent support for Vue SFCs. However, do note as of now its support for ` ``` @@ -140,6 +172,8 @@ watchEffect( The `flush` option also accepts `'sync'`, which forces the effect to always trigger synchronously. This is however inefficient and should be rarely needed. +In Vue >= 3.2.0, `watchPostEffect` and `watchSyncEffect` aliases can also be used to make the code intention more obvious. + ### Watcher Debugging The `onTrack` and `onTrigger` options can be used to debug a watcher's behavior. @@ -241,22 +275,14 @@ const state = reactive({ watch( () => state, (state, prevState) => { - console.log( - 'not deep', - state.attributes.name, - prevState.attributes.name - ) + console.log('not deep', state.attributes.name, prevState.attributes.name) } ) watch( () => state, (state, prevState) => { - console.log( - 'deep', - state.attributes.name, - prevState.attributes.name - ) + console.log('deep', state.attributes.name, prevState.attributes.name) }, { deep: true } ) @@ -279,10 +305,7 @@ const state = reactive({ watch( () => _.cloneDeep(state), (state, prevState) => { - console.log( - state.attributes.name, - prevState.attributes.name - ) + console.log(state.attributes.name, prevState.attributes.name) } ) diff --git a/src/guide/single-file-component.md b/src/guide/single-file-component.md index e0e7a7db85..5078f7823a 100644 --- a/src/guide/single-file-component.md +++ b/src/guide/single-file-component.md @@ -2,173 +2,85 @@ ## Introduction -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. +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: -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: - -- **Global definitions** force unique names for every component -- **String templates** lack syntax highlighting and require ugly slashes for multiline HTML -- **No CSS support** means that while HTML and JavaScript are modularized into components, CSS is conspicuously left out -- **No build step** restricts us to HTML and ES5 JavaScript, rather than preprocessors like Pug (formerly Jade) and Babel - -All of these are solved by **single-file components** with a `.vue` extension, made possible with build tools such as Webpack or Browserify. - -Here's an example of a file we'll call `Hello.vue`: - - - -Now we get: - -- [Complete syntax highlighting](https://github.com/vuejs/awesome-vue#source-code-editing) -- [CommonJS modules](https://webpack.js.org/concepts/modules/#what-is-a-webpack-module) -- [Component-scoped CSS](https://vue-loader.vuejs.org/en/features/scoped-css.html) - -As promised, we can also use preprocessors such as Pug, Babel (with ES2015 modules), and Stylus for cleaner and more feature-rich components. - - - -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. - -### What About Separation of Concerns? - -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. - -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: +```vue + -```html - -{{ greeting }}
- - -``` - -## Getting Started - -### Example Sandbox - -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. - -### For Users New to Module Build Systems in JavaScript - -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: -- **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. - -- **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. - -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! - -### For Advanced Users - -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/). - -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). - -### Building with rollup - -Most of the time when developing a third-party library we want to build it in a way that allows the consumers of the library to [tree shake](https://webpack.js.org/guides/tree-shaking/) it. To enable tree-shaking we need to build `esm` modules. Since webpack and, in turn, vue-cli do not support building `esm` modules we need to rely on [rollup](https://rollupjs.org/). - -#### Installing Rollup - -We will need to install Rollup and a few dependencies: - -```bash -npm install --save-dev rollup @rollup/plugin-commonjs rollup-plugin-vue + ``` -These are the minimal amount of rollup plugins that we need to use to compile the code in an `esm` module. We may want to also add [rollup-plugin-babel](https://github.com/rollup/plugins/tree/master/packages/babel) to transpile their code and [node-resolve](https://github.com/rollup/plugins/tree/master/packages/node-resolve) if we use dependencies that we want to bundle with our library. +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: ``, `