Home > Web Front-end > Vue.js > The most systematic Vue complete set of tutorials (detailed explanations and examples)

The most systematic Vue complete set of tutorials (detailed explanations and examples)

WBOY
Release: 2021-12-23 19:05:12
forward
5991 people have browsed it

This article brings you a complete set of tutorials about vue, including practical examples. I hope it will be helpful to everyone.

The most systematic Vue complete set of tutorials (detailed explanations and examples)

Vue (pronounced /vjuː/, similar to View) is a progressive framework for building user interfaces. Unlike other large frameworks, Vue is designed to be applied layer by layer from the bottom up. Vue's core library only focuses on the view layer, which is not only easy to get started, but also easy to integrate with third-party libraries or existing projects. On the other hand, when combined with a modern tool chain and various supporting libraries, Vue is fully capable of providing drivers for complex single-page applications.

1. MVVM mode and the first Vue program

1. What is MVVM

  • This layer performs two-way data binding upwards with the view layer
  • Downward to interact with the Model layer through interface requests for data interaction
    The most systematic Vue complete set of tutorials (detailed explanations and examples)
    Vue installation method:

1.1 Use the CDN method (beginners Use)
 It can also be introduced directly using CDN. The code is as follows:

<script></script>
Copy after login
Copy after login

∣ 1.2Vue-cli scaffolding
∣ Use Vue-cli scaffolding to build a Vue project, which will be explained in detail in the seventh point below. (Recommended for medium and large projects).

2. The first Vue program

1. Create an HTML file

2.Introduce Vue.js

<script></script>
Copy after login
Copy after login

Complete example:

nbsp;html>
    
    贝西说
    
    <script></script>
 

           {{message}}  

 <script> var vue=new Vue({ el:"#app", /*model数据*/ data:{ message:"hello,vue" } }); </script>
Copy after login

Demonstration effect: (view-driven data, data-driven view)
The most systematic Vue complete set of tutorials (detailed explanations and examples)

2. Basic syntax

v-bind

v-bind is used to bind data and element attributes
Complete example:

    <p>
        <a>点我</a>
    </p>
 <script>
     var app = new Vue({
         el:&#39;.app&#39;,
         data:{
             url:"https://www.baidu.com",
         }
     });
 </script>
Copy after login

Note:
  v-bind is followed by: attribute name =. My understanding is that it means binding this attribute. After binding, the corresponding value must be found in the vue data.
  When we change the url in the console, the corresponding response will also change.
Similarly, we can also bind the image src attribute and the class of the hyperlink

    <p>
        <a>点我</a>
        <img  alt="The most systematic Vue complete set of tutorials (detailed explanations and examples)" >
    </p>
 <script>
     var app = new Vue({
         el:&#39;.app&#39;,
         data:{
             url:"https://www.baidu.com",
             imgsrc:"https://cn.vuejs.org/images/logo.png"
         }
     });
 </script>
Copy after login

Note:

<p>
	  <a>点我</a></p>
Copy after login

Usually we can abbreviate v-bind: as:

<p>
        <a>点我</a></p>
Copy after login

v-if,v-else

##v-if,v-else Full example:

    <p>
        </p><p>YES</p>
        <p>NO</p>
    
     <script>
         var app = new Vue({
             el:"#app",
             data:{
                 ok:true,
             }
         });
     </script>
Copy after login

v-if,v-else-if,v-else

    <p>
        </p><p>您好,admin</p>
        <p>贾志杰</p>
        <p>您无权访问!</p>
    
     <script>
         var app = new Vue({
             el:"#app",
             data:{
                 role:"admin",
             }
         });
     </script>
Copy after login

v-for

## 1. v-for loop ordinary array
    <p>
        </p><p>{{item}}----索引:{{index}}</p>
    
     <script>
         var app = new Vue({
             el:"#app",
             data:{
                list:[1,2,3,4,5],
             }
         });
     </script>
Copy after login

2. v-for loop object array

    <p>
        </p><p>{{user.id}}---{{user.name}}-----索引:{{index}}</p>
    
     <script>
         var app = new Vue({
             el:"#app",
             data:{
                list:[
                    {id:1,name:&#39;beixi&#39;},
                    {id:2,name:&#39;jzj&#39;},
                    {id:3,name:&#39;贾志杰&#39;}
                ],
             }
         });
     </script>
Copy after login

3. v-for loop object

    <p>
        </p><p>值:{{val}}---键:{{key}}-----索引:{{index}}</p>
    
     <script>
         var app = new Vue({
             el:"#app",
             data:{
                user:{
                    name:"beixi",
                    age:"18",
                    sex:"男"
                }
             }
         });
     </script>
Copy after login

4. v-for loop numbers

    <p>
        </p><p>这是第{{count}}次循环</p>
    
     <script>
         var app = new Vue({
             el:"#app",
             data:{ }
         });
     </script>
Copy after login

3. Vue binding event

Syntax:

v-on: event name = "method name"
Abbreviation: @event name = "method name"

Event name: click|keydown|keyup|mouseover|mouseout|custom event name

v-on event monitoring, complete example:
nbsp;html>
    
    贝西说
    
    <script></script>
    

       {{count}}                       

     <script> var app = new Vue({ el:"#app", data:{count:1 }, methods:{ sub:function(){ this.count-=1; } } }); </script>
Copy after login

Note: v-bind can be abbreviated as: v-on: can be abbreviated as @

4. Vue : Form double binding, component

1. What is two-way data binding

Vue.js is an MVVM framework, that is, two-way data binding, that is, when the data changes, the view also When the view changes, the data will also change synchronously. This can be regarded as the essence of Vue.js.

It is worth noting that the two-way data binding we are talking about must be for UI controls. Non-UI controls will not involve two-way data binding. One-way data binding is a prerequisite for using state management tools. If we use vuex, the data flow is also single-item, which will conflict with two-way data binding.

2. Use two-way data binding in the form

You can use the

v-model

directive to create two-way data binding on the form , and elements. It automatically chooses the correct method to update the element based on the control type. Despite its magic, v-model is essentially syntactic sugar. It is responsible for listening to user input events to update data and perform some special processing for some extreme scenarios. # ## Note: v-model will ignore the initial values ​​of the value, checked, and selected attributes of all form elements and always use the data of the Vue instance as the data source. You should declare the initial value in the data option of the component via JavaScript!

Example 1:


    <p>
        <input>{{message}}
    </p>
     <script>
         var app = new Vue({
             el:"#app",
             data:{message:&#39;&#39; }
         });
     </script>
Copy after login
Complete effect:


Example 2 :The most systematic Vue complete set of tutorials (detailed explanations and examples)

  <p>
        <input>男        <input>女        </p><p>{{gender}}</p>
    
     <script>
         var app = new Vue({
             el:"#app",
             data:{gender:&#39;&#39; }
         });
     </script>
Copy after login
Example 3:

    <p>
        <select>
            <option>--请选择--</option>
            <option>北京</option>
            <option>上海</option>
            <option>广州</option>
        </select>
        </p><p>{{selected}}</p>
    
     <script>
         var app = new Vue({
             el:"#app",
             data:{selected:&#39;&#39; }
         });
     </script>
Copy after login

3.什么是组件

  组件是可复用的 Vue 实例,说白了就是一组可以重复使用的模板,跟 JSTL 的自定义标签、Thymeleaf 的 th:fragment 等框架有着异曲同工之妙。
  通常一个应用会以一棵嵌套的组件树的形式来组织:The most systematic Vue complete set of tutorials (detailed explanations and examples)
例如,你可能会有页头、侧边栏、内容区等组件,每个组件又包含了其它的像导航链接、博文之类的组件。

4.简单定义一个组件

注意:在实际开发中,我们并不会用以下方式开发组件,而是采用 vue-cli 创建 .vue 模板文件的方式开发,以下方法只是为了让大家理解什么是组件。

  <p>
        <beixi></beixi>
    </p>
     <script>
 	    //注册组件
         Vue.component("beixi",{
             template:&#39;<li>hello&#39;
         });
         var app = new Vue({
             el:"#app",
         });
     </script>
Copy after login

说明:

  • Vue.component():注册组件
  • beixi:自定义组件的名字
  • template:组件的模板

5.使用 props 属性动态传递参数

    <p>
        <!--组件:使用props把值传递给组件-->
        <blog-post></blog-post>
    </p>
     <script>
         Vue.component("blog-post",{
             props:[&#39;value&#39;],
             template:&#39;<li>{{value}}&#39;
         });
         var app = new Vue({
             el:"#app",
             data:{
                 items:[&#39;beixi&#39;,&#39;jzj&#39;,&#39;贾志杰&#39;]
             }
         });
     </script>
Copy after login

说明:

v-for=“item in items”:遍历 Vue 实例中定义的名为 items 的数组,并创建同等数量的组件
v-bind:value=“item”:将遍历的 item 项绑定到组件中 props 定义的名为 value属性上;= 号左边的 value 为 props 定义的属性名,右边的为 item in items 中遍历的 item 项的值

五、Axios异步通信

1.什么是Axios

  Axios 是一个开源的可以用在浏览器端和 NodeJS 的异步通信框架,她的主要作用就是实现 AJAX 异步通信,其功能特点如下:

  • 从浏览器中创建 XMLHttpRequests
  • 从 node.js 创建 http 请求
  • 支持 Promise API [JS中链式编程]
  • 拦截请求和响应
  • 转换请求数据和响应数据
  • 取消请求
  • 自动转换 JSON 数据
  • 客户端支持防御 XSRF(跨站请求伪造)

  GitHub:https://github.com/axios/axios

  中文文档:http://www.axios-js.com/

2.为什么要使用 Axios

  由于 Vue.js 是一个 视图层框架 并且作者(尤雨溪)严格准守 SoC (关注度分离原则),所以 Vue.js 并不包含 AJAX 的通信功能,为了解决通信问题,作者单独开发了一个名为 vue-resource 的插件,不过在进入 2.0 版本以后停止了对该插件的维护并推荐了 Axios 框架。少用jQuery,因为它操作Dom太频繁!

3.第一个 Axios 应用程序

  咱们开发的接口大部分都是采用 JSON 格式,可以先在项目里模拟一段 JSON 数据,数据内容如下:创建一个名为 data.json 的文件并填入上面的内容,放在项目的根目录下

{
  "name": "贝西说",
  "url": "https://blog.csdn.net/beixishuo",
  "page": 1,
  "isNonProfit": true,
  "address": {
    "street": "太谷",
    "city": "山西晋中",
    "country": "中国"
  },
  "links": [
    {
      "name": "bilibili",
      "url": "https://space.bilibili.com/474668932"
    },
    {
      "name": "贝西说",
      "url": "https://blog.csdn.net/beixishuo"
    },
    {
      "name": "百度",
      "url": "https://www.baidu.com/"
    }
  ]}
Copy after login

完整示例:

    
    <script></script>
    
    <script></script>

    

{{info.name}}

    

{{info.address}}

    贝西说<script> var app=new Vue({ el:"#app", //data: 属性 data:function(){//需要处理(返回)后台数据在页面上渲染时使用 return{ //请求的返回参数格式必须和json字符串一样 info:{ name:null,//相当于形参占位,实际参数data.json会赋予 url:null, address:{ street:null, city:null, country:null } } } }, mounted:function(){//mounted钩子函数,相当于java中main函数。可以调用methods中定义的函数 // axios.get(&#39;data.json&#39;).then(resp=>(console.log(resp.data))); axios.get(&#39;data.json&#39;).then(resp=>(this.info=resp.data)); } })</script>
Copy after login

注意:
读取本地json文件中的数据时读取失败,如图

The most systematic Vue complete set of tutorials (detailed explanations and examples)
解决方式就是右击浏览器快捷方式,更改属性,在目标后面加上

–allow-file-access-from-files
The most systematic Vue complete set of tutorials (detailed explanations and examples)

说明:

  1. 在这里使用了 v-bind 将 a:href 的属性值与 Vue 实例中的数据进行绑定
  2. 使用 axios 框架的 get 方法请求 AJAX 并自动将数据封装进了 Vue 实例的数据对象中
  3. 我们在data中的数据结构必须要和Ajax响应回来的数据格式匹配!

4.axios API

可以通过将相关配置传递给 axios 来进行请求。

axios(config)

// 发送一个 POST 请求axios({
	method: 'post',
	url: '/user/12345',
	data: {
	firstName: 'Fred',
	lastName: 'Flintstone'
	}});
Copy after login

axios(url[, config])

// 发送一个 GET 请求 (GET请求是默认请求模式)
	axios('/user/12345');
Copy after login

请求方法别名
为了方便起见,已经为所有支持的请求方法提供了别名。

  • axios.request(config)

  • axios.get(url [,config])

  • axios.delete(url [,config])

  • axios.head(url [,config])

  • axios.post(url [,data [,config]])

  • axios.put(url [,data [,config]])

  • axios.patch(url [,data [,config]])

注意

当使用别名方法时,不需要在config中指定url,method和data属性。

5.Vue的生命周期

The most systematic Vue complete set of tutorials (detailed explanations and examples)

六、计算属性

1.什么是计算属性

  当一些数据需要根据其它数据变化时,需要进行处理才能去展示,虽然vue提供了绑定数据表达式绑定的方式,但是设计它的初衷只是用于简单运算的。在模板中放入太多的逻辑会让模板过重且难以维护,对于一些比较复杂和特殊的计算有可能就捉襟见肘了,而且计算的属性写在模板里也不利于项目维护

computed主要的作用:

  •   分离逻辑(模板和数据分离)
  •   缓存值
  •   双向绑定(getter,setter)

简单理解为:把计算的结果当作属性返回去

2.完整示例

    
    <script></script>

         

求和结果{{result}}

<script> var app=new Vue({ el:"#app", data:{num1:1,num2:2}, computed:{//计算属性 result:function(){ return parseInt(this.num1)+parseInt(this.num2); } } })</script>
Copy after login

3、methods方法与computed计算属性区别

  1. 两者的执行结果是完全相同的
  2. 计算属性是基于他们的依赖进行缓存的,只有在相关依赖发生改变时,他们才会重新求值,也就是说,只要他的依赖没有发生变化,那么每次访问的时候计算属性都会立即返回之前的计算结果,不再执行函数
  3. 每次触发重新渲染时,调用方法将总会再次执行函数

七、前端工程化 vue-cli

 Vue脚手架指的是vue-cli,它是一个专门为单页面应用快速搭建繁杂的脚手架,它可以轻松的创建新的应用程序而且可用于自动生成vue和webpack的项目模板。
 利用vue-cli脚手架来构建Vue项目需要先安装Node.js和NPM环境。
1.Node.js的安装
Node.js的安装比较简单,大家需要在node.js官网(https://nodejs.org/en/download/)下载并安装node.js环境,windows的推荐下载Windows Installer (.msi)。同时,大家会得到一个附送的NPM工具。

  • 安装Node.js,双击下载好的node文件,如图所示。
    The most systematic Vue complete set of tutorials (detailed explanations and examples)
    安装过程比较简单,一直“下一步”即可。
  • 环境变量配置:安装完成后需要设置环境变量:即Path中添加安装目录(例如:D:\java\nodejs),如图所示。
    The most systematic Vue complete set of tutorials (detailed explanations and examples)
  • 点击开始=》运行=》输入"cmd" => 输入node -v如图所示,验证安装是否成功。
    The most systematic Vue complete set of tutorials (detailed explanations and examples)
    2.npm安装
    由于node.js已经集成了npm,所以之前npm也一并安装好了。所以在cmd终端输入npm -v 来测试是否安装成功。命令如图 所示,出现版本提示表示安装成功。
    The most systematic Vue complete set of tutorials (detailed explanations and examples)
    3基本使用
    The most systematic Vue complete set of tutorials (detailed explanations and examples)
    步骤如下:
    ① 搭建第一个完整的Vue-cli 脚手架构建的项目。
    The most systematic Vue complete set of tutorials (detailed explanations and examples)
    ② 安装完成,输入Vue -V,如果出现相应的版本号,则说明安装成功。如图7-6所示。
    The most systematic Vue complete set of tutorials (detailed explanations and examples)
    ③ 我们可以使用vue-cli来快速生成一个基于webpack模板构建的项目,如图所示,项目名为vue-project。
    The most systematic Vue complete set of tutorials (detailed explanations and examples)
    ④ 配置完成后,可以看到目录下多出了一个项目文件夹,里面就是 vue-cli 创建的一个基于 webpack 的 vue.js 项目。
    然后进入项目目录(如:cd vue-project),使用 npm install安装依赖,如图所示。
    The most systematic Vue complete set of tutorials (detailed explanations and examples)
    依赖安装完成后,我们来看一下项目的目录结构,如下所示:
.|-- build                            // 项目构建(webpack)相关代码|   |-- build.js                     // 生产环境构建代码|   |-- check-version.js             // 检查node、npm等版本|   |-- dev-client.js                // 热重载相关|   |-- dev-server.js                // 构建本地服务器|   |-- utils.js                     // 构建工具相关|   |-- webpack.base.conf.js         // webpack基础配置|   |-- webpack.dev.conf.js          // webpack开发环境配置|   |-- webpack.prod.conf.js         // webpack生产环境配置|-- config                           // 项目开发环境配置|   |-- dev.env.js                   // 开发环境变量|   |-- index.js                     // 项目一些配置变量|   |-- prod.env.js                  // 生产环境变量|   |-- test.env.js                  // 测试环境变量|-- node_modules		   //所需要依赖资源|-- src                              // 源码目录|   |--  assets                   	   //存放资产文件|   |-- components                   // vue公共组件|   |-- router                   	//存放路由js文件,用于页面的跳转|   |-- App.vue                        // 页面入口文件|   |-- main.js                        // 程序入口文件,加载各种公共组件|-- static                           // 静态文件,比如一些图片,json数据等|   |-- data                           // 群聊分析得到的数据用于数据可视化|-- .babelrc                         // ES6语法编译配置|-- .editorconfig                    // 定义代码格式|-- .gitignore                       // git上传需要忽略的文件格式|-- README.md                        // 项目说明|-- favicon.ico 
|-- index.html                       // 入口页面|-- package.json                     // 项目基本信息.
Copy after login

对于开发者更多操作的是src目录:

|-- src                              // 源码目录|   |--  assets                   	   //存放资产文件|   |-- components                   // vue公共组件|   |-- router                   	//存放路由js文件,用于页面的跳转|   |-- App.vue                        // 页面入口文件|   |-- main.js
Copy after login

④ 输入npm run dev命令来启动项目,如图所示。
The most systematic Vue complete set of tutorials (detailed explanations and examples)

运行成功后在浏览器输入:http://localhost:8080,访问项目结果如图所示。
The most systematic Vue complete set of tutorials (detailed explanations and examples)

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of The most systematic Vue complete set of tutorials (detailed explanations and examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
vue
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template