Home > php教程 > PHP开发 > body text

Getting started with Vue.js

高洛峰
Release: 2016-11-30 16:53:37
Original
1713 people have browsed it

Introduction

vue.js is a client-side js library that can be used to develop single-page applications. In order to select a project, I looked at angular, react, and vuejs one after another. I admired the first two and loved the latter. Because it is simple and clean, and it also has advanced web components implementation. Even if there isn't much documentation, I'd choose it. Next, we first create a starting project and go over the concepts and components involved in the development process.

vue.js

The development process of decent vuejs is almost always combined with webpack and babel. For those who like to hack from scratch, let me tell you that the configuration is extremely cumbersome. Fortunately, vue.js provides a tool called vue-cli. Can be used to quickly build the starting code for a single-page application. It only takes a minute to launch commonly used development features:

Scaffolding code available.

Hot reload. Automatically reload after component code is updated

Static code inspection.

ES6 language features

Tool preparation

We need to use vue-cli to create a scaffolding project.

Install vue-cli

$ npm install -g vue-cli
Copy after login

Confirm the node version

My version is

$ node -v

v5.0.0

$ npm -v

3.10.6

Many problems may occur if they occur It depends on the version, the suggestion is the same as mine.

Create a new project

Execution:

$ vue init webpack my-project
Copy after login

The second parameter webpack specifies to create a vuejs project based on the "webpack" template. This template will create a webpack scaffolding code.

However, what is webpack? It itself is a packaging tool that can package js, css, and image into one or more js files, and can support various loaders as plug-ins to convert different types of files. In fact, webpack uses the plug-in vue-loader to perform format conversion when loading Vue type files, and translates Vue type files into js files that the browser can recognize.

Attention for Chinese users: The vue init command uses npm. The npm warehouse is often slow or blocked. You can use domestic mirrors. Just edit ~/.npmrc and add the following content:

registry = https://registry.npm.taobao.org
Copy after login

This method can be much faster.

The currently available templates are:

webpack - Through webpack and the vue-loader plug-in, you can call babel to compile the .vue file into a js file that the client can recognize. Hot loading, code inspection, and testing can also be provided by default.

webpack-simple - The simplest webpack and vue-loader plugin.

browserify - Through the combination of Browserify + vueify, babel can be called to compile the .vue file into a js file that can be recognized by the client. Hot loading, code inspection, and testing can also be provided by default.

browserify-simple - The simplest Browserify + vueify plugin.

Theoretically, webpack and browserify have similar functions, and both can be used as packaging tools. But webpack is that popular tool that has very little documentation, but everyone is vying to use it. So, let’s not worry about it and use webpack first.

Install dependencies, go to http://localhost:8080 to check the effect.

View vue files

vue files are a trinity. That is to say, css, html, and js are all in one file, and they are separated using tags. In order to better view the structure, it is recommended to first install the highlight plug-in corresponding to the editor.

Install syntax highlighting

The editor I am accustomed to using is sublime text. Installing the plug-in can identify all vuejs component codes with the extension .vue and give them highlights to facilitate code reading and writing. This plug-in is called vue-syntax-highlight and is officially provided by vuejs. It's located at github.com. Just clone it into your Sublime package directory. On my computer, the Sublime package directory is /Users/lcj/Library/Application Support/Sublime Text 3/Packages, so the installation process is

$ cd my-project
$ npm install
$ npm run dev
Copy after login

and then restart. After reading the code, all files with a .vue extension will have corresponding highlights.

View vue

There is a component code in the starting code, which is in src/hello.vue. View:

cd /Users/lcj/Library/Application\ Support/Sublime\ Text\ 3/Packages
git clone https://github.com/vuejs/vue-syntax-highlight
Copy after login

The file is divided into three parts. The