- Full-Stack Vue.js 2 and Laravel 5
- Anthony Gore
- 281字
- 2021-07-02 19:57:23
Lifecycle hooks
When your main script is run and your instance of Vue is set up, it goes through a series of initialization steps. As we said earlier, Vue will walk through your data objects and make them reactive, as well as compile the template and mount to the DOM. Later in the lifecycle, Vue will also go through updating steps, and later still, tear-down steps.
Here is a diagram of the lifecycle instance taken fromhttp://vuejs.org. Many of these steps concern concepts that we haven't yet covered, but you should get the gist:

Figure 2.11. Vue.js lifecycle diagram
Vue allows you to execute custom logic at these different steps vialifecycle hooks,which are callbacks defined in the configuration object.
For example, here we utilize thebeforeCreateandcreatedhooks:
new Vue({ data: { message: 'Hello' }, beforeCreate: function() { console.log('beforeCreate message: ' + this.message); // "beforeCreate message: undefined" }, created: function() { console.log('created: '+ this.message); // "created message: Hello" }, });
Vue will alias data properties to the context objectafterthebeforeCreatehook is called butbeforethecreatedhook is called, hence whythis.messageisundefinedin the former.
The caveat I mentioned earlier about theEscapekey listener is this: although unlikely, if theEscapekey was pressed and our callback was calledbeforeVue has proxied the data properties,app.modalOpenwould beundefinedrather thantrueand so ourifstatement would not control flow like we expect.
To overcome this we can set up the listener in thecreatedlifecycle hook that will be calledafterVue has proxied the data properties. This gives us a guarantee thatmodalOpenwill be defined when the callback is run.
app.js:
function escapeKeyListener(evt) { if (evt.keyCode === 27 && app.modalOpen) { app.modalOpen = false; } } var app = new Vue({ data: { ... }, watch: { ... }, created: function() { document.addEventListener('keyup', escapeKeyListener); } });
- Python概率統(tǒng)計(jì)
- Monkey Game Development:Beginner's Guide
- Unity 2020 Mobile Game Development
- AngularJS Web Application Development Blueprints
- Python高級(jí)編程
- Magento 2 Development Cookbook
- Learning Neo4j 3.x(Second Edition)
- ASP.NET 3.5程序設(shè)計(jì)與項(xiàng)目實(shí)踐
- Hands-On GPU:Accelerated Computer Vision with OpenCV and CUDA
- Python機(jī)器學(xué)習(xí)編程與實(shí)戰(zhàn)
- Mastering ServiceNow(Second Edition)
- Learning OpenStack Networking(Neutron)(Second Edition)
- Terraform:多云、混合云環(huán)境下實(shí)現(xiàn)基礎(chǔ)設(shè)施即代碼(第2版)
- ElasticSearch Cookbook(Second Edition)
- Java Web從入門(mén)到精通(第3版)