Home Web Front-end JS Tutorial Detailed interpretation of vue reconstruction technology

Detailed interpretation of vue reconstruction technology

Jun 09, 2018 pm 05:59 PM

This article mainly introduces the key points and summary of Vue project reconstruction technology. Now I will share it with you and give you a reference.

Preface

I’ve been too busy recently and haven’t updated my blog for a long time. Taking a break from my busy schedule today, I would like to briefly summarize some of the technical points of the recent Vue project reconstruction.

vue data is updated, but the view is not updated

We often encounter this problem. Generally, when vue data is assigned, the vue data changes, but the view is not updated. . This is not the technical point of project reconstruction, let’s share with you the usual solution of vue2.0!

The solution is as follows:

1. Assign value through vue.set method

Vue.set(数据源, key, newValue)
Copy after login

2. Through Array.prototype.splice method

Data source.splice(indexOfItem, 1, newValue)

3. Modify the length of data

Data source.splice(newLength)

4.Mutation method

Vue.js wraps the mutation methods of the observed array, so they can trigger view updates. The wrapped methods are:

push()
pop()
shift()
unshift()
splice()
sort()
reverse()
Copy after login

prop object array application

In JavaScript, objects and arrays are reference types, pointing to the same memory space. If prop is an object or array, changing it inside a child component will affect the state of the parent component. Taking advantage of this, when we change the prop array or object in the child component, the parent component and all places where the data in the prop is applied will change. I have written an article about js deep copy and shallow copy before. If you are interested, take a look. In fact, the principles are the same.

The case is as follows:

<input class="pinput max" type="text" v-model="itemData.data.did">

<script>
export default {
 components: {
 },
 data() {
 },
 props: {
 itemData: Object
 },
 methods: {
 }
};
</script>
Copy after login

All places applied to itemData will change!

Vue will not give a warning in the console for the above change of prop. If we completely change or assign a prop, the console will give a warning! Quoting the official solution as follows:

1. Define a local variable and initialize it with the value of prop:

props: [&#39;initialCounter&#39;],
data: function () {
 return { counter: this.initialCounter }
}
Copy after login

2. Define a calculated property, process the value of prop and return:

props: [&#39;size&#39;],
computed: {
 normalizedSize: function () {
 return this.size.trim().toLowerCase()
 }
}
Copy after login

Some pitfalls of v-model

In fact, v-model and sync are some syntactic sugars. I have introduced them in articles before, and you can also find similar ones on the official website. Case!

v-model Sometimes when the data is undefined, no error will be reported, so be sure to note that v-model cannot be undefined, otherwise there will be some inexplicable problems!

Refactoring - Creation of dynamic components

Sometimes we have many similar components with only a little difference. We can write such similar components in In the configuration file, dynamically create and reference components

Method 1: Use component with is

By using reserved elements and dynamically binding their is attributes , you can dynamically switch multiple components on the same mount point:

var vm = new Vue({
 el: &#39;#example&#39;,
 data: {
 currentView: &#39;home&#39;
 },
 components: {
 home: { /* ... */ },
 posts: { /* ... */ },
 archive: { /* ... */ }
 }
})
<component v-bind:is="currentView">
 <!-- 组件在 vm.currentview 变化时改变! -->
</component>
Copy after login

Method 2: Create

<script>
export default {
 data() {
 return {
 };
 },
 render: function(createElement) {
 let _type = bi.chart.data.type;
 let _attr = bi.chart.components[_type]["attr"];
 return createElement(_attr, {
  props: {
  }
 });
 }
};
</script>
Copy after login

bi.chart.components[_type] through the render method ["attr"] This is dynamically configured in the configuration file. When type is clicked, it will change and the attr attribute under different types will be taken!

Public attribute extraction

In our projects, we often use a lot of states or data. We can extract a lot of public data and put it into an object. , we can monitor changes in this data object later. Save or obtain data.

c: {
 handler: function (val, oldVal) { /* ... */ },
 deep: true
},
// 该回调将会在侦听开始之后被立即调用
d: {
 handler: function (val, oldVal) { /* ... */ },
 immediate: true
},
Copy after login

You can use the above depth monitoring. If it needs to be executed immediately during initialization, we can use immediate execution monitoring!

require dynamic loading of dependencies

We can use the require synchronization feature to dynamically load dependencies in the code. For example, in the echart theme below, we can dynamically load it when we click to switch. !

require("echarts/theme/"+ data.theme);
Copy after login

The import loading should be placed in the head. During initialization, the default theme can be loaded with import!

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Get the value method of the default selected radio button in angularjs (detailed tutorial)

Through angularJS How to use radio to achieve one of the two options (detailed tutorial)

Comprehensive interpretation of cli in vue (detailed tutorial)

The details are Your interpretation of vue-cli 3.0 new features (detailed tutorial)

The above is the detailed content of Detailed interpretation of vue reconstruction technology. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles