Detailed introduction to Vue structure (with examples)
This article brings you a detailed introduction to the Vue structure (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1.Vue usage scenarios:
* 在html中通过script引入 * 在webpack中,由于配置有处理各种文件的loader,所以可以用import引入
2.Vue instructions
v-cloak v-bind ==> : v-on ==> @ v-text v-html v-modal :class : 简单值,数组,对象,数组中对象 :style : 数组, 对象 v-for v-if v-show
Customized global directive
Vue.derictive(自定义指令名字, 指令生效周期配置对象{ bind : (被绑定的那个元素的js原生对象el) => {}, ==> 一旦绑上马上调用 inserted : (el同上) => {}, ==> 元素插入到DOM之后再调用 updated : (el同上, binding) => {} }
Customized local directive
var vm2 = new Vue({ el: '#app2', data: {}, methods: {}, directives: { 'fontweight': { bind: function (el, binding) { el.style.fontWeight = binding.value } }, } })
3.Vue event modifier
.stop .prevent .capture .self .once .self和.stop在阻止冒泡行为上的区别
4.Vue filter
The parameter passing rules of global filter are the same;
If global filter and local filter The name of the filter is the same. When using a filter inside a component, its own internal filter will be used first
Global filter
Vue.filter('自定义过滤器名字' , function(第一参数来自于管道符 | 前的数据 , 后面的参数是该过滤器被调用时候传递过来的参数){}
Local filter
is defined in the filter attribute in the Vue instance
var vm2 = new Vue({ el: '#app2', data: {}, methods: {}, filters: { dateFormat: function (dateStr, pattern = '') {} } }, })
5.Vue key modifier
Key modifiers, like event modifiers like .self, are used after v-on events.
.enter .tab .up .down .left .right .delete .esc .space
Custom global key modifier
Vue.config.keyCodes.自定义按键修饰符名字 = 按键的码值
6.Vue component life cycle
beforeCreated(){} ==> data和methods中数据还没初始化好 created(){} ==> data和methods中的数据已经初始化好 beforeMount(){} ==> 模板已经在内存中编译好了,但是没有被渲染到页面上 mounted(){} ==> 内存中的模板已经渲染到了页面行,用户可以看到页面了 beforeUpdate (){} ==> data中的数据已经更新,但是界面上的数据没有更新 updated (){} ==> 界面上数据更新结束 beforeDestory (){} ==> 此时data和methods中数据还可以使用 destoryed (){} ==> data和methods中数据都不能使用了,Vnode被完全销毁
7.Vue-resource
Similar to axios, but depends on Vue.js. After introduction, the $http attribute is automatically added to the Vue instance
$http.get() $http.post() $http.jsonp() 均返回一个promise this.$http.get('http://vue.studyit.io/api/getlunbo').then(function (result) { console.log(result.body) })
8. Vue animation
Class name
v-enter v-enter-active v-enter-to v-leave v-leave-active v-leave-to
Use third-party class name
<transition name='my' enter-active-class="bounceIn" leave-active-class="bounceOut" :duration="{ enter: 200, leave: 400 }"> <h3 v-if="flag" class="animated">这是一个H3</h3> </transition>
Animation hook function
<transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter"> <p class="ball" v-show="flag"></p> </transition> beforeEnter (要执行动画的那个对象的原生DOM对象el) {} ==> 动画入场之前,设置动画的起始样式 enter (el , done) {done()} ==> 动画开始后样式,设置动画结束样式 afterEnter () {} ==> 动画完全结束回调
9.Vue creates components
(1) Component template object creation
方式1: Vue.extend({ template : '' }) 方式2: 直接一个 Object
(2) Register global component
Vue.component(自定义组件名字, 组件模板)
-
(3) The difference between component data and Vue instance data
Component data must return object
10. Component switching and animation
Component switching
<component :is="comName"></component> comName是变量
Animation when component switching
<!-- 通过 mode 属性,设置组件切换时候的 模式 --> <transition mode="out-in"> <component :is="comName"></component> </transition>
11. Passing values between parent and child components
Passing values from parent components to child components
传单纯值: <com1 v-bind:parentmsg="msg"></com1> 传函数 <com2 @func="show"></com2> this.$emit('func', this.sonmsg)
The child component passes the value to the parent component
Pass the parameters of the function through the parent component
$refs
12.Vue routing
Same as Vue-resource, you need to introduce a vue-router.js file
let routeObj = new VueRouter({ routes : [ {path : '' , redrect : '' , component : null} ] }) var vm = new Vue({ el: '#app', router: routerObj });
Routing parameters
<router-link to="/login?id=10&name=zs">login</router-link> routes: [ { path: '/login/:id/:name', component: login , children : [{path , redrect , component , children}] }, ] $route.params.id
One route corresponds to multiple components
<router-view></router-view> <p class="container"> <router-view name="left"></router-view> <router-view name="main"></router-view> </p> var router = new VueRouter({ routes: [ { path: '/', components: { 'default': header, 'left': leftBox, 'main': mainBox } } ] })
13.watch monitors data changes or routing changes in data
var vm = new Vue({ el: '#app', data: {firstname: '',}, watch: { // 使用这个 属性,可以监视 data 中指定数据的变化,然后触发这个 watch 中对应的 function 处理函数 'firstname': function (newVal, oldVal) { this.fullname = newVal + '-' + this.lastname }, '$route.path': function (newVal, oldVal) { ==> 只需要这是一个变量 if (newVal === '/login') { console.log('欢迎进入登录页面') } else if (newVal === '/register') { console.log('欢迎进入注册页面') } } } });
The above is the detailed content of Detailed introduction to Vue structure (with examples). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).
