The life cycle of Vue components and Route (detailed tutorial)
This article mainly introduces the knowledge related to the life cycle of Vue components and Route. Friends who need it can refer to it
Let’s talk about some practical things first
Practical Point hook:
Created: A life cycle hook function after the vue instance is generated. (Page initialization data loading is generally written here);
beforeCreate: Give a loading interface created to cancel loading;
beforeDestory: Are you sure to delete XX?
destoryed: The current component has been deleted and the related content has been cleared
About the Vue component life cycle, the translated icon:
module.exports = { //props: ['父组建传的值'], data:function(){ lifecycle.push("data"); return { msg: '各个阶段,可以查看控制台输出,message from my-views', title:'my_views', lifecycle: lifecycle } }, //这里是route的生存周期 route:{ //waitForData: true, // 数据加载完毕后再切换试图,也就是 点击之后先没反应,然后数据加载完,再出发过渡效果 canActivate:function(transition){ // canActivate阶段,可以做一些用户验证的事情(是否可以被激活) // 在验证阶段,当一个组件将要被切入的时候被调用。 }, activate:function(transition){ // 在激活阶段被调用,在 activate 被断定( resolved ,指该函数返回的 promise 被 resolve )。用于加载和设置当前组件的数据。(激活) //this.$root.$set('header',this.title); transition.next(); //此方法结束后,api会调用afterActivate 方法 //在aftefActivate中 会给组件添加 $loadingRouteData 属性 并设置为true }, data: function(transition) { var _this = this; // 在激活阶段被调用,在 activate 被断定( resolved ,指该函数返回的 promise 被 resolve )。用于加载和设置当前组件的数据 // 说明之前请求过 则不用再请求了 if(this.$root.myViewsData){ this.$data = this.$root.myViewsData; transition.next(); console.log('已经请求过了不再请求数据'); return; } //将数据同步到根节点 this.$root.myViewsData = this.$data; setTimeout(function(){ //这里 _this.$loadingRouteData 是 true transition.next({msg:'加载后的数据'}); //在调用完transition.next 后,_this.$loadingRouteData 为 false }.bind(this),4000); }, canDeactivate:function(transition){ // 在验证阶段,当一个组件将要被切出的时候被调用。(是否可以被禁用) }, deactivate: function (transition) { // 在激活阶段,当一个组件将要被禁用和移除之时被调用。(禁用) } }, beforeCreate:function(){ // 在实例初始化之后,数据观测(data observer) 和 event/watcher 事件配置之前被调用。 }, created:function(){ // 实例已经创建完成之后被调用。在这一步,实例已完成以下的配置:数据观测(data observer),属性和方法的运算, watch/event 事件回调。然而,挂载阶段还没开始,$el 属性目前不可见。 }, beforeCreate:function(){ // 在实例初始化之后,数据观测(data observer) 和 event/watcher 事件配置之前被调用。 }, mounted:function(){ // el 被新创建的 vm.$el 替换,并挂载到实例上去之后调用该钩子。如果 root 实例挂载了一个文档内元素,当 mounted 被调用时 vm.$el 也在文档内。 }, beforeUpdate: function(){ // 数据更新时调用,发生在虚拟 DOM 重新渲染和打补丁之前。 // 你可以在这个钩子中进一步地更改状态,这不会触发附加的重渲染过程。 }, updated: function(){ // 由于数据更改导致的虚拟 DOM 重新渲染和打补丁,在这之后会调用该钩子。 //当这个钩子被调用时,组件 DOM 已经更新,所以你现在可以执行依赖于 DOM 的操作。然而在大多数情况下,你应该避免在此期间更改状态,因为这可能会导致更新无限循环。 }, // <keep-alive> 包裹动态组件时,会缓存不活动的组件实例,而不是销毁它们。 activated: function(){ // keep-alive 组件激活时调用。 }, deactivated: function(){ // keep-alive 组件停用时调用。 }, beforeDestroy:function(){ // 实例销毁之前调用。在这一步,实例仍然完全可用。 }, destroyed:function(){ // Vue 实例销毁后调用。调用后,Vue 实例指示的所有东西都会解绑定,所有的事件监听器会被移除,所有的子实例也会被销毁。 } }
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Returning non-callback methods through WebView processing in react-native
Using Elememt-UI build management in Vue Backend (detailed tutorial)
What are the methods of using isPlainObject() in jQuery?
The above is the detailed content of The life cycle of Vue components and Route (detailed tutorial). 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



The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

Implement marquee/text scrolling effects in Vue, using CSS animations or third-party libraries. This article introduces how to use CSS animation: create scroll text and wrap text with <div>. Define CSS animations and set overflow: hidden, width, and animation. Define keyframes, set transform: translateX() at the beginning and end of the animation. Adjust animation properties such as duration, scroll speed, and direction.

In Vue.js, lazy loading allows components or resources to be loaded dynamically as needed, reducing initial page loading time and improving performance. The specific implementation method includes using <keep-alive> and <component is> components. It should be noted that lazy loading can cause FOUC (splash screen) issues and should be used only for components that need lazy loading to avoid unnecessary performance overhead.

You can query the Vue version by using Vue Devtools to view the Vue tab in the browser's console. Use npm to run the "npm list -g vue" command. Find the Vue item in the "dependencies" object of the package.json file. For Vue CLI projects, run the "vue --version" command. Check the version information in the <script> tag in the HTML file that refers to the Vue file.
