Home  >  Article  >  Web Front-end  >  Small and beautiful Vue project in action: lightweight Vue and Webpack applications

Small and beautiful Vue project in action: lightweight Vue and Webpack applications

WBOY
WBOYOriginal
2023-06-09 16:07:191422browse

The rise of the Vue framework has changed the way of front-end development. Its simplicity, ease of use, efficiency and flexibility have been recognized by the majority of developers. As a powerful module packaging tool, Webpack also plays an important role in the development of front-end engineering. This article will introduce a small and beautiful Vue project in practice. It is developed using lightweight Vue.js and Webpack. It can be quickly started and provides a learning reference for beginners.

  1. Prerequisite knowledge

Before studying this article, we need to have the following knowledge reserves:

• Basic HTML, CSS, and JavaScript development capabilities;

• Basic knowledge of Vue.js and the use of common APIs;

• Basic knowledge of Webpack and the use of common configuration items.

If you are not familiar with the above knowledge, it is recommended to carry out basic study and practice first.

  1. Project development process

We will develop the project from the following steps:

• Initialize the project

• Install dependencies

• Configure Webpack

• Design page layout

• Write Vue components

• Package the project

Next, let’s Step by step into the application development journey of Vue and Webpack.

  1. Initialize the project

Using Vue-cli can quickly generate the skeleton of the Vue.js project, and presets some common configuration items to facilitate our rapid development. Here we use Vue-cli to initialize the project.

The first step is to install the Vue-cli tool:

npm install -g @vue/cli

The second step is to create a new project with Vue-cli and enter the working directory on the command line:

vue create vue-webpack-project

The project we created here is named vue-webpack-project, and Vue-cli will generate a project folder named vue-webpack-project in the current directory.

  1. Installing dependencies

Enter the project root directory and run the following command to install the required dependencies:

npm install vue vue-router --save

The dependencies we need to install here include Vue.js And Vue-router, Vue-router is a routing plug-in officially provided by Vue.js, which can easily handle front-end routing related issues.

  1. Configuring Webpack

In actual development, Webpack configuration is usually more complicated than writing code, so we only need to configure some common configuration items.

The first step is to create a new webpack.config.js file to store the configuration of Webpack:

const path = require('path');
const { VueLoaderPlugin } = require('vue-loader');

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /.vue$/,
        use: 'vue-loader'
      },
      {
        test: /.js$/,
        use: 'babel-loader'
      },
      {
        test: /.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ]
      }
    ]
  },
  plugins: [
    new VueLoaderPlugin()
  ]
};

Here we configure three rules: vue-loader for processing .vue files, babel-loader that processes .js files, css-loader and vue-style-loader plugins that process .css files.

The second step is to modify the package.json file and add a command in the scripts attribute to run Webpack in the project root directory:

{
  "scripts": {
    "build": "webpack"
  },
  ……
}
  1. Design the page layout

Before project development, we need to design the page layout first. Here we use the ElementUI component library for rapid development and use the components directly in the HTML file.

<!DOCTYPE html>
<html>
  <head>
    <title>vue-webpack-project</title>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  </head>
  <body>
    <div id="app">
      <el-container>
        <el-header>Header</el-header>
        <el-container>
          <el-aside>Aside</el-aside>
          <el-main>Main</el-main>
        </el-container>
      </el-container>
    </div>
    <script src="./dist/bundle.js"></script>
  </body>
</html>
  1. Writing Vue components

In the src directory, create two new .vue files: Header.vue and Main.vue, the code is as follows:

Header.vue

<template>
  <el-header>
    <h1>Header</h1>
  </el-header>
</template>

<script>
export default {
}
</script>

<style scoped>
el-header {
  text-align: center;
  color: #fff;
  background-color: #409EFF;
}
</style>

Main.vue

<template>
  <el-main>
    <h1>Main</h1>
  </el-main>
</template>

<script>
export default {
}
</script>

<style scoped>
el-main {
  text-align: center;
}
</style>

Here we use ElementUI components to implement the layout of Header and Main.

  1. Packaging the project

In the command line, run the following command to package the Webpack:

npm run build

Webpack will package the project according to our configuration , generate the dist directory and bundle.js file.

  1. Summary

This article introduces the use of lightweight Vue.js and Webpack through a small but beautiful Vue project, including initializing the project, installing dependencies, Steps such as configuring Webpack, designing page layout, writing Vue components, and packaging projects. As a beginner of Vue and Webpack, you can refer to this article to practice and learn some basic usage and configuration, and deepen your understanding of Vue.js and Webpack.

The above is the detailed content of Small and beautiful Vue project in action: lightweight Vue and Webpack applications. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn