search
HomeWeb Front-endJS TutorialDetailed explanation of the use of vue.js progressive framework

This time I will bring you a detailed explanation of the use of the vue.js progressive framework. What are the precautions for the detailed explanation of the vue.js progressive framework? The following is a practical case, let's take a look.

Vue.js is a progressive framework for building user interfaces. Unlike other heavyweight frameworks, Vue Fundamentally adopt minimal cost, incrementally adoptable design. Vue The core library only focuses on the view layer and is easy to integrate with other third-party libraries or existing projects. On the other hand, when combined with single-file components and libraries supported by the Vue ecosystem, Vue It is also fully capable of providing powerful drivers for complex single-page applications.

Vue.js has been updated to 2.x, with certain upgrades and modifications in functions and syntax. This article first introduces the basic content.

1, Beginner’s Guide

The use of vue is very simple. Download vue.js or vue.min.js and import it directly.

2. Initial introduction to vue

2.1 Declarative Rendering

The core of Vue.js is that you can use concise template syntax to declaratively render data into DOM:

<p>
 {{ message }}
</p>
var app = new Vue({
 el: '#app',
 data: {
 message: 'Hello Vue!'
 }
})

 This will input: Hello Vue!

We have generated our first Vue application! This looks very similar to rendering a string template, but Vue does a lot of work behind the scenes. Now data and DOM Already tied together, all data and DOM are reactive. How do we understand all this clearly? Just turn on JavaScript in your browser Console (now open on the current page) and set the value of app.message, you will see that the DOM element rendered by the example above will update accordingly.

 In addition to text interpolation, we can also bind DOM element attributes in this way:

<p>
 <span>
 鼠标悬停此处几秒,
 可以看到此处动态绑定的 title!
 </span>
</p>
var app2 = new Vue({
 el: '#app-2',
 data: {
 message: '页面加载于 ' + new Date().toLocaleString()  }
})

 After hovering the mouse for a few seconds, you can see dynamic prompts.

Here we encounter something new. The v-bind attributes you see are called directives. Directives are prefixed with v- to indicate that they are generated by Vue Special properties provided. As you might have guessed, they produce specialized reactive behavior on the rendered DOM. In short, what this directive does here is: "match the title attribute of this element with The message property of the Vue instance keeps the association updated."

If you open the browser's JavaScript console again and enter app2.message = 'Some new message', you will see again that the HTML bound to the title attribute has been updated.

2.1 Conditions and loops

Controlling the display of an element is also quite simple:

<p>
 </p><p>现在你可以看到我</p>

var app3 = new Vue({
 el: '#app-3',
 data: {
 seen: true
 }
})

ˆContinue typing app3.seen = false in the console and you should see span disappear.

This example shows that we can not only bind data to text and attributes, but also to DOM structures. Moreover, Vue also provides a powerful Transition Effect system, which can automatically use transition effects when Vue inserts/updates/delete elements.  There are other commands, each with their own special functions. For example, the v-for directive can use data in an array to display a list of items:

<p>
 </p><ol>
 <li>
  {{ todo.text }}
 </li>
 </ol>

var app4 = new Vue({
 el: '#app-4',
 data: {
 todos: [
  { text: '学习 JavaScript' },
  { text: '学习 Vue' },
  { text: '创建激动人心的代码' }
 ]
 }
})

3, vue instance  Every Vue application starts by creating a new Vue instance through the Vue function:

var vm = new Vue({
 // 选项
})

Even though it does not fully follow the MVVM pattern, Vue’s design is still inspired by it. As a convention, we usually use the variable vm (short for ViewModel) to represent a Vue instance.

3.1data and methods

 When creating a Vue instance, all properties found in the data object are added to Vue's reactive system. Whenever the values ​​of these properties change, the view is "responsive" and updates with the corresponding new values.

// data 对象
var data = { a: 1 }
// 此对象将会添加到 Vue 实例上
var vm = new Vue({
 data: data
})
// 这里引用了同一个对象!
vm.a === data.a // => true
// 设置实例上的属性,
// 也会影响原始数据
vm.a = 2
data.a // => 2
// ... 反之亦然
data.a = 3
vm.a // => 3

Every time the data object changes, the view will be triggered to re-render. It is worth noting that if the instance has been created, only those properties that already exist in data are reactive. That is to say, if after the instance is created, a new attribute is added, for example:

vm.b = 'hi'

  然后,修改 b 不会触发任何视图更新。如果你已经提前知道,之后将会用到一个开始是空的或不存在的属性,你就需要预先设置一些初始值。例如:

data: {
 newTodoText: '',
 visitCount: 0,
 hideCompletedTodos: false,
 todos: [],
 error: null
}

  除了 data 属性, Vue 实例还暴露了一些有用的实例属性和方法。这些属性与方法都具有前缀 $,以便与用户定义(user-defined)的属性有所区分。例如:

var data = { a: 1 }
var vm = new Vue({
 el: '#example',
 data: data
})
vm.$data === data // => true
vm.$el === document.getElementById('example') // => true
// $watch 是一个实例方法
vm.$watch('a', function (newValue, oldValue) {
 // 此回调函数将在 `vm.a` 改变后调用
})

3.2vue实例的声明周期

  vue实例的声明周期是一个很重要的概念,理解之后可以通过它实现很多功能。

  看下这段代码。

nbsp;html>

 
  <meta>
  <title></title>
 
 
  <p>我的声明周期,大家看吧!</p>
 
 <script></script>
 <script></script>
 <script>
  //以下代码时显示vm整个生命周期的流程
  var vm = new Vue({
   el: "#container",
   data: {
    test : &#39;hello world&#39;
   },
   beforeCreate: function(){
    console.log(this);
    showData(&#39;创建vue实例前&#39;,this);
   },
   created: function(){
    showData(&#39;创建vue实例后&#39;,this);
   },
   beforeMount:function(){
    showData(&#39;挂载到dom前&#39;,this);
   },
   mounted: function(){
    showData(&#39;挂载到dom后&#39;,this);
   },
   beforeUpdate:function(){
    showData(&#39;数据变化更新前&#39;,this);
   },
   updated:function(){
    showData(&#39;数据变化更新后&#39;,this);
   },
   beforeDestroy:function(){
    vm.test ="3333";
    showData(&#39;vue实例销毁前&#39;,this);
   },
   destroyed:function(){
    showData(&#39;vue实例销毁后&#39;,this);
   }
  });
  function realDom(){
   console.log(&#39;真实dom结构:&#39; + document.getElementById(&#39;container&#39;).innerHTML);
  }
  function showData(process,obj){
   console.log(process);
   console.log(&#39;data 数据:&#39; + obj.test)
   console.log(&#39;挂载的对象:&#39;)
   console.log(obj.$el)
   realDom();
   console.log(&#39;------------------&#39;)
   console.log(&#39;------------------&#39;)
  }
 </script>

  通过控制台的打印效果可以看出来,实例化 vue 对象大致分为 创建vue实例、挂载到dom、数据变化更新、vue实例销毁 4个阶段,,注意每个阶段都有对应的钩子,我们可以通过对这些钩子进行操作,达成一些功能。虽然初学者用不太上,但是提前了解一下还是好的,到时候碰到实际功能要能想得到生命周期的钩子。         

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

ajax与jsonp的使用详解

Vue 2.0内部指令

前端开发中的多列布局实现方法

The above is the detailed content of Detailed explanation of the use of vue.js progressive framework. 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
Javascript Data Types : Is there any difference between Browser and NodeJs?Javascript Data Types : Is there any difference between Browser and NodeJs?May 14, 2025 am 12:15 AM

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment