Prise en main de Vue

Présentons Maintenant Vue, le troisième de nos cadres. Dans cet article, nous allons examiner un peu de fond Vue, apprendre à l'installer et créer un nouveau projet, étudier la structure de haut niveau de l'ensemble du projet et un composant individuel, voir comment exécuter le projet localement, et le préparer à commencer à construire notre exemple.

Conditions préalables:

Familiarité avec les langages HTML, CSSet JavaScript, connaissance de la ligne terminal/commande.

Les composants Vue sont écrits sous la forme d'une combinaison d'objets JavaScript qui gèrent les données de l'application et d'une syntaxe de modèle html qui cartographie la structure DOM sous-jacente. Pour l'installation et pour utiliser certaines des fonctionnalités les plus avancées de Vue (comme les composants de fichier unique ou les fonctions de rendu), vous aurez besoin d'un terminal avec nœud + npm installé.

Objectif: Pour configurer un environnement de développement Vue local, créez une application de démarrage et comprenez les bases de son fonctionnement.

Une Vue plus claire

Vue est un cadre JavaScript moderne qui fournit des installations utiles pour une amélioration progressive - contrairement à beaucoup d'autres cadres, vous pouvez utiliser Vue pour améliorer html existant. Cela vous permet d'utiliser Vue comme un remplacement de drop-in pour une bibliothèque comme JQuery.

Cela étant dit, vous pouvez également utiliser Vue pour écrire des applications à page unique entières (SPA). Cela vous permet de créer un balisage géré entièrement par Vue, ce qui peut améliorer l'expérience et les performances des développeurs lors de la gestion d'applications complexes. Il vous permet également de profiter des bibliothèques pour le routage côté client et la gestion de l'État lorsque vous en avez besoin. En outre, Vue adopte une approche « intermédiaire » pour l'outillage comme le routage côté client et la gestion de l'État. Bien que l'équipe de base de Vue gère des bibliothèques suggérées pour ces fonctions, elles ne sont pas directement regroupées dans Vue. Cela vous permet de sélectionner une bibliothèque de routage/gestion d'état différente si elle correspond mieux à votre application.

En plus de vous permettre d'intégrer progressivement Vue dans vos applications, Vue propose également une approche progressive du balisage de l'écriture. Comme la plupart des cadres, Vue vous permet de créer des blocs réutilisables de balisage via des composants. La plupart du temps, les composants Vue sont écrits à l'aide d'une syntaxe de modèle HTML spéciale. Lorsque vous avez besoin de plus de contrôle que ne le permet la syntaxe HTML, vous pouvez écrire des fonctions JSX ou JavaScript simples pour définir vos composants.

Au fur et à mesure que vous travaillez sur ce didacticiel, vous pouvez garder le guide Vue et la documentation API ouverts dans d'autres onglets, de sorte que vous pouvez vous référer à eux si vous voulez plus d'informations sur n'importe quel sous-sujet. Pour une bonne comparaison (mais potentiellement biaisée) entre Vue et plusieurs des autres cadres, voir Vue Docs: Comparison with Other Frameworks.

Installation

Pour utiliser Vue dans un site existant, vous pouvez déposer l'un des éléments suivants sur une page. Cela vous permet de commencer à utiliser Vue sur les sites existants, c'est pourquoi Vue se targue d'être un cadre progressif. Il s'agit d'une excellente option lors de la migration d'un projet existant à l'aide d'une bibliothèque comme JQuery à Vue. Avec cette méthode, vous pouvez utiliser un grand nombre des fonctionnalités de base de Vue, telles que les attributs, les composants personnalisés et la gestion des données.<script>

  • Script de développement (Non optimisé, mais inclut les avertissements de console. Idéal pour le développement
    html
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    
  • Production Script (Optimized version, minimal console warnings. It is recommended that you specify a version number when including Vue on your site so that any framework updates do not break your live site without you knowing.)
    html
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    

However, this approach has some limitations. To build more complex apps, you'll want to use the Vue NPM package. 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:

  1. Node.js 8.11+ installed.
  2. npm or yarn.

Note : If you don't have the above installed, find out more about installing npm and Node.js here.

To install the CLI, run the following command in your terminal:

bash
npm install --global @vue/cli

Or if you'd prefer to use yarn:

bash
yarn global add @vue/cli

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 . 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. vue create <project-name>

We'll look at using this 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. Follow the steps below:

  1. In terminal, to where you'd like to create your sample app, then run cd vue create moz-todo-vue
  2. Use the arrow keys and to select the "Manually select features" option. Enter
  3. The first menu you'll be presented with allows you to choose which features you want to include in your project. Make sure that "Babel" and "Linter / Formatter" are selected. If they are not, use the arrow keys and the space bar to toggle them on. Once they are selected, press to proceed. Enter
  4. Next you'll select a config for the linter / formatter. Navigate to "Eslint with error prevention only" and hit again. This will help us catch common errors, but not be overly opinionated. Enter
  5. Next you are asked to configure what kind of automated linting we want. Select "Lint on save". This will check for errors when we save a file inside the project. Hit to continue. Enter
  6. Now, you will select how we want your config files to be managed. "In dedicated config files" will put your config settings for things like ESLint into their own, dedicated files. The other option, "In package.json", will put all of your config settings into the app's file. Select "In dedicated config files" and push package.json. Enter
  7. Finally, you are asked if you want to save this as a preset for future options. This is entirely up to you. If you like these settings over the existing presets and want to use them again, type , otherwise type . y n

The CLI will now begin scaffolding out your project, and installing all of your dependencies.

If you've never run the Vue CLI before, you'll get one more question — you'll be asked to choose a package manager. You can use the arrow keys to select which one you prefer. 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=<package-manager>, 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.

Note : We've not gone over all of the options here, but you can find more information on the CLI in the Vue docs.

Project structure

If everything went successfully, the CLI should have created a series of files and directories for your project. The most significant ones are as follows:

  • .eslintrc.js: This is a config file for eslint. You can use this to manage your linting rules.
  • babel.config.js: This is the config file for Babel, 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.
  • .browserslistrc: This is a config for Browserslist. You can use this to control which browsers your tooling optimizes for.
  • public: This directory contains static assets that are published, but not processed by Webpack during build (with one exception; gets some processing) index.html.
    • 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.

  • 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 file your app should be attached to. This file is often where you register global components or additional Vue libraries.index.html
    • App.vue: this is the top-level component in your Vue app. See below for more explanation of Vue components.
    • components: this directory is where you keep your components. Currently it just has one example component.
    • assets: This directory is for storing static assets like CSS and images. Because these files are in the source directory, they can be processed by Webpack. This means you can use pre-processors like Sass/SCSS or Stylus.

Note : Depending on the options you select when creating a new project, there might be other directories present (for example, if you choose a router, you will also have a directory).views

.vue files (single file components)

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, 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.

Let's explore this now.

App.vue

Open your App.vue file — you'll see that it has three parts: <template>, <script>, and <style>, which contain the component's template, scripting, and styling information. All Single File Components share this same basic structure.

<template> contains all the markup structure and display logic of your component. Your template can contain any valid HTML, as well as some Vue-specific syntax that we'll cover later.

Note: By setting the lang attribute on the <template> tag, you can use Pug template syntax instead of standard HTML — <template lang="pug">. We'll stick to standard HTML through this tutorial, but it is worth knowing that this is possible.

<script> contains all of the non-display logic of your component. Most importantly, your <script> tag needs to have a default exported JS object. This object is where you locally register components, define component inputs (props), handle local state, define methods, and more. Your build step will process this object and transform it (with your template) into a Vue component with a render() function.

In the case of App.vue, our default export sets the name of the component to App and registers the HelloWorld component by adding it into the components property. When you register a component in this way, you're registering it locally. Locally registered components can only be used inside the components that register them, so you need to import and register them in every component file that uses them. This can be useful for bundle splitting/tree shaking since not every page in your app necessarily needs every component.

js
import HelloWorld from "./components/HelloWorld.vue";

export default {
  name: "app",
  components: {
    //You can register components locally here.
    HelloWorld,
  },
};

Note: If you want to use TypeScript syntax, you need to set the lang attribute on the <script> tag to signify to the compiler that you're using TypeScript — <script lang="ts">.

<style> is where you write your CSS for the component. If you add a scoped attribute — <style scoped> — Vue will scope the styles to the contents of your SFC. This works similar to CSS-in-JS solutions, but allows you to just write plain CSS.

Note: If you select a CSS pre-processor when creating the project via the CLI, you can add a lang attribute to the <style> tag so that the contents can be processed by Webpack at build time. For example, <style lang="scss"> will allow you to use SCSS syntax in your styling information.

Running the app locally

The Vue CLI comes with a built-in development server. This allows you to run your app locally so you can test it easily without needing to configure a server yourself. The CLI adds a serve command to the project's package.json file as an npm script, so you can easily run it.

In your terminal, try running npm run serve (or yarn serve if you prefer yarn). Your terminal should output something like the following:

INFO  Starting development server...
98% after emitting CopyPlugin

  DONE  Compiled successfully in 18121ms

  App running at:
  - Local:   <http://localhost:8080/>
  - Network: <http://192.168.1.9:8080/>

  Note that the development build is not optimized.
  To create a production build, run npm run build.

If you navigate to the "local" address in a new browser tab (this should be something like http://localhost:8080 as stated above, but may vary based on your setup), you should see your app. Right now, it should contain a welcome message, a link to the Vue documentation, links to the plugins you added when you initialized the app with your CLI, and some other useful links to the Vue community and ecosystem.

default Vue app render, with Vue logo, welcome message, and some documentation links

Making a couple of changes

Let's make our first change to the app — we'll delete the Vue logo. Open the App.vue file, and delete the <img> element from the template section:

html
<img alt="Vue logo" src="./assets/logo.png" />

If your server is still running, you should see the logo removed from the rendered site almost instantly. Let's also remove the HelloWorld component from our template.

First of all delete this line:

html
<HelloWorld msg="Welcome to Your Vue.js App" />

If you save your App.vue file now, the rendered app will throw an error because we've registered the component but are not using it. We also need to remove the lines from inside the <script> element that import and register the component:

Delete these lines now:

js
import HelloWorld from "./components/HelloWorld.vue";
js
components: {
  HelloWorld;
}

Your rendered app should no longer show an error, just a blank page, as we currently have no visible content inside <template>.

Let's add a new <h1> inside <div id="app">. Since we're going to be creating a todo list app below, let's set our header text to "To-Do List". Add it like so:

html
<template>
  <div id="app">
    <h1>To-Do List</h1>
  </div>
</template>

App.vue will now show our heading, as you'd expect.

Summary

Let's leave this here for now. We've learnt about some of the ideas behind Vue, created some scaffolding for our example app to live inside, inspected it, and made a few preliminary changes.

With a basic introduction out of the way, we'll now go further and build up our sample app, a basic Todo list application that allows us to store a list of items, check them off when done, and filter the list by all, complete, and incomplete todos.

In the next article we'll build our first custom component, and look at some important concepts such as passing props into it and saving its data state.