diff --git a/.vscode/project-words.txt b/.vscode/project-words.txt index b8d9554c7fa11c0..9d083c5af289fd8 100644 --- a/.vscode/project-words.txt +++ b/.vscode/project-words.txt @@ -58,6 +58,7 @@ okan Oklab oklch Paciello +Pinia prophoto rgba ripgrep diff --git a/files/en-us/learn/tools_and_testing/client-side_javascript_frameworks/vue_first_component/index.md b/files/en-us/learn/tools_and_testing/client-side_javascript_frameworks/vue_first_component/index.md index 8890e0554b5f96f..75ad85375f9deba 100644 --- a/files/en-us/learn/tools_and_testing/client-side_javascript_frameworks/vue_first_component/index.md +++ b/files/en-us/learn/tools_and_testing/client-side_javascript_frameworks/vue_first_component/index.md @@ -307,26 +307,26 @@ Try changing `true` to `false` and back again, reloading your app in between to Great! We now have a working checkbox where we can set the state programmatically. However, we can currently only add one `ToDoList` component to the page because the `id` is hardcoded. This would result in errors with assistive technology since the `id` is needed to correctly map labels to their checkboxes. To fix this, we can programmatically set the `id` in the component data. -We can use the [lodash](https://www.npmjs.com/package/lodash) package's `uniqueid()` method to help keep the index unique. This package exports a function that takes in a string and appends a unique integer to the end of the prefix. This will be sufficient for keeping component `id`s unique. +We can use the [nanoid](https://github.com/ai/nanoid) package to help keep the index unique. This package exports a function `nanoid()` that generates a unique string. This will be sufficient for keeping component `id`s unique. Let's add the package to our project with npm; stop your server and enter the following command into your terminal: ```bash -npm install --save lodash.uniqueid +npm install --save nanoid ``` -> **Note:** If you prefer yarn, you could instead use `yarn add lodash.uniqueid`. +> **Note:** If you prefer yarn, you could instead use `yarn add nanoid`. We can now import this package into our `ToDoItem` component. Add the following line at the top of `ToDoItem.vue`'s ` ``` -However, this approach has some limitations. To build more complex apps, you'll want to use the [Vue npm package](https://www.npmjs.com/package/vue). This will let you use advanced features of Vue and take advantage of bundlers like WebPack. To make building apps with Vue easier, there is a CLI to streamline the development process. To use the npm package & the CLI you will need: +However, this approach has some limitations. To build more complex apps, you'll want to use the [Vue npm package](https://www.npmjs.com/package/vue). This will let you use advanced features of Vue and use tools like Vite or WebPack. To make building apps with Vue easier, there is a CLI scaffolding tool [create-vue](https://github.com/vuejs/create-vue) to streamline the development process. To use `create-vue` you will need: -1. Node.js 8.11+ installed. +1. Node.js 20 installed. 2. [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) or [yarn](https://yarnpkg.com/). > **Note:** If you don't have the above installed, find out [more about installing npm and Node.js](/en-US/docs/Learn/Tools_and_testing/Understanding_client-side_tools/Command_line#adding_powerups) here. -To install the CLI, run the following command in your terminal: +To install Vue and initialize a new project, run the following command in your terminal: ```bash -npm install --global @vue/cli +npm create vue@latest ``` Or if you'd prefer to use yarn: ```bash -yarn global add @vue/cli +yarn create vue@latest ``` -Once installed, to initialize a new project you can then open a terminal in the directory you want to create the project in, and run `vue create `. The CLI will then give you a list of project configurations you can use. There are a few preset ones, and you can make your own. These options let you configure things like TypeScript, linting, vue-router, testing, and more. - -We'll look at using this below. +This command will give you a list of project configurations you can use. There are a few defaults, but you may pick your own project-specific settings. These options let you configure things like TypeScript, linting, vue-router, testing, and more. +We'll step through the options in the initialization steps below. ## Initializing a new project -To explore various features of Vue, we will be building up a sample todo list app. We'll begin by using the Vue CLI to create a new app framework to build our app into. +To explore various features of Vue, we will be building up a sample todo list app. We'll begin by using `create-vue` to build a new scaffold for our app. +In terminal, `cd` to where you'd like to create your sample app, then run `npm create vue@latest` (or `yarn create vue@latest` if you prefer Yarn). + +The interactive tool let's you choose some options and you can procees by pressing Enter. +For this project, we'll use the following configuration: -In terminal, `cd` to where you'd like to create your sample app, then run `vue create moz-todo-vue`. -You can choose the default option `Default ([Vue 3] babel, eslint)` by pressing Enter to proceed. -The CLI will now begin scaffolding out your project, and installing all of your dependencies. +```plain +✔ Project name: … todo-vue +✔ Add TypeScript? … No +✔ Add JSX Support? … No +✔ Add Vue Router for Single Page Application development? … No +✔ Add Pinia for state management? … No +✔ Add Vitest for Unit Testing? … No +✔ Add an End-to-End Testing Solution? › No +✔ Add ESLint for code quality? … Yes +? Add Prettier for code formatting? › Yes +``` -If you've never run the Vue CLI before, you'll get one more question — you'll be asked to choose a package manager which defaults to `yarn`. -The Vue CLI will default to this package manager from now on. If you need to use a different package manager after this, you can pass in a flag `--packageManager=`, when you run `vue create`. So if you wanted to create the `moz-todo-vue` project with npm and you'd previously chosen yarn, you'd run `vue create moz-todo-vue --packageManager=npm`. +After choosing these options, your project structure is now configured and dependencies are defined in a `package.json` file. +The next steps are to install the dependencies and start the server, and the tool conveniently prints out the commands you need to do this: -> **Note:** We've not gone over all of the options here, but you can [find more information on the CLI](https://cli.vuejs.org) in the Vue docs. +```plain +Scaffolding project in /path/to/todo-vue... + +Done. Now run: + + cd todo-vue + npm install + npm run format + npm run dev +``` ## Project structure @@ -111,15 +133,12 @@ If everything went successfully, the CLI should have created a series of files a - `package.json`: This file contains the list of dependencies for your project, as well as some metadata and `eslint` configuration. - `yarn.lock`: If you chose `yarn` as your package manager, this file will be generated with a list of all the dependencies and sub-dependencies that your project needs. -- `babel.config.js`: This is the config file for [Babel](https://babeljs.io/), which transforms modern JavaScript features being used in development code into older syntax that is more cross-browser compatible in production code. You can register additional babel plugins in this file. - `jsconfig.json`: This is a config file for [Visual Studio Code](https://code.visualstudio.com/docs/languages/jsconfig) and gives context for VS Code on your project structure and assists auto-completion. -- `public`: This directory contains static assets that are published, but not processed by [Webpack](https://webpack.js.org/) during build (with one exception; `index.html` gets some processing). - +- `vite.config.js`: This is the configuration file for the [Vite](https://vitejs.dev/) development server that builds and serves your project on your local machine. + The Vite server watches source files for changes and can hot-reload the project while you make changes. +- `public`: This directory contains static assets that are published during build. - `favicon.ico`: This is the favicon for your app. Currently, it's the Vue logo. - - `index.html`: This is the template for your app. Your Vue app is run from this HTML page, and you can use lodash template syntax to interpolate values into it. - - > **Note:** this is not the template for managing the layout of your application — this template is for managing static HTML that sits outside of your Vue app. Editing this file typically only occurs in advanced use cases. - +- `index.html`: Your Vue app is run from this HTML page. - `src`: This directory contains the core of your Vue app. - `main.js`: this is the entry point to your application. Currently, this file initializes your Vue application and signifies which HTML element in the `index.html` file your app should be attached to. This file is often where you register global components or additional Vue libraries. @@ -133,11 +152,9 @@ If everything went successfully, the CLI should have created a series of files a Like in many front-end frameworks, components are a central part of building apps in Vue. These components let you break a large application into discrete building blocks that can be created and managed separately, and transfer data between each other as required. These small blocks can help you reason about and test your code. -While some frameworks encourage you to separate your template, logic, and styling code into separate files, Vue takes the opposite approach. Using [Single File Components (SFC)](https://vuejs.org/guide/scaling-up/sfc.html), Vue lets you group your templates, corresponding script, and CSS all together in a single file ending in `.vue`. These files are processed by a JS build tool (such as Webpack), which means you can take advantage of build-time tooling in your project. This allows you to use tools like Babel, TypeScript, SCSS and more to create more sophisticated components. - -As a bonus, projects created with the Vue CLI are configured to use `.vue` files with Webpack out of the box. In fact, if you look inside the `src` folder in the project we created with the CLI, you'll see your first `.vue` file: `App.vue`. +While some frameworks encourage you to separate your template, logic, and styling code into separate files, Vue takes the opposite approach. Using [Single File Components (SFC)](https://vuejs.org/guide/scaling-up/sfc.html), Vue lets you group your templates, corresponding script, and CSS all together in a single file ending in `.vue`. These files are processed by a JS build tool (such as Vite or Webpack), which means you can take advantage of build-time tooling in your project. This allows you to use tools like Babel, TypeScript, SCSS and more to create more sophisticated components. -Let's explore this now. +Let's look inside the `src` folder in the project we created with the CLI and inspect your first `.vue` file: `App.vue`. ### App.vue @@ -147,69 +164,61 @@ Open your `App.vue` file — you'll see that it has three parts: `