Vue development experience and skills sharing
This series records some of the experiences and techniques I summarized during one year of Vue development.
Use Object.freeze() to improve performance
Object.freeze() is a new feature of ES5, which can freeze an object to prevent the object from being modified.
vue 1.0.18+ provides support for it. For objects frozen using freeze in data or vuex, vue will not convert getters and setters.
If you have a huge array or Object, and you are sure that the data will not be modified, using Object.freeze() can greatly improve performance. In my actual development, this improvement is about 5 to 10 times, and the multiple increases with the amount of data.
And, Object.freeze() freezes the value, you can still replace the variable reference. For example:
{{ item.value }}
new Vue({ data: { // vue不会对list里的object做getter、setter绑定 list: Object.freeze([ { value: 1 }, { value: 2 } ]) }, created () { // 界面不会有响应 this.list[0].value = 100; // 下面两种做法,界面都会响应 this.list = [ { value: 100 }, { value: 200 } ]; this.list = Object.freeze([ { value: 100 }, { value: 200 } ]); } })
vue’s documentation does not write this feature, but this This is a very practical approach. For big data that is purely for display, Object.freeze can be used to improve performance.
Use vm.$compile to compile dom
The $compile function can be used to manually call vue to compile dom. This function can come in handy when you need to process the html generated by a jQuery plug-in or the html returned by the server. But note that this is a private API and may change at any time, and this approach goes against the philosophy of Vue. Use only as a last resort.
new Vue({ data: { value: 'demo' }, created () { let dom = document.createElement('div'); dom.innerHTML = '{{ value }}'; this.$compile(dom); } })
Reasonable use of track-by="$index"
track-by is an optimization method provided by vue for loops, which can reuse the dom with the same ID in v-for multiple times. If your data does not have a unique id, you can also choose to use track-by="$index", but you must be aware of some side effects.
For example:
new Vue({ data: { list: [1, 2, 3] } }) <div id="demo-1"> <p v-for="item in list">{{ item }}</p> </div> <div id="demo-2"> <p v-for="item in list" track-by="$index">{{ item }}</p> </div>
At this time, execute this.list = [4, 5, 6], you can observe through F12 that all the dom in demo-1 is deleted, and then the list is re-circulated to generate the dom , and demo-2 will not delete the dom, but just change their text grid to 4, 5, 6. This is the effect of track-by="$index", which reuses the dom with the same $index in v-for twice.
This is a good optimization method, but it is not applicable to all scenarios. For example, when the loop contains form controls or subcomponents, since the dom will not be deleted and regenerated, v-for will be executed for the second time. The value of the original form control will not change. You can see this example: https://jsfiddle.net/jysboza9/1/
Don’t abuse Directive
There is a saying on the Internet that all DOM operations should be encapsulated in instructions. In actual development, I think this dogma should not be followed. Whether to use instructions should depend on what function you implement, not whether the dom is manipulated. For example, if you want to use vue to encapsulate a jQuery plug-in, let's see which of the following encapsulation methods is better:
<!-- component --> <datepicker></datepicker> <!-- directive --> <div v-datepicker="{options}"></div>
Personally, I think the first method is undoubtedly better. Datepicker is an independent component. You don't You need to care about whether it operates DOM internally and whether it encapsulates the jQuery plug-in.
So when to use commands? Let’s take a look at the commands provided natively by the browser:
<a title="这是一个指令"></a> <p title="这是一个指令"></p> <div title="这是一个指令"></div>
The title attribute provides tooltip functions for different tags. This is a command. A directive should represent an independent function that can provide the same functionality to different tags and components.

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

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

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











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.

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.

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

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.

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.

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values for each element; and the .map method can convert array elements into new arrays.

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema
