


About the operations of extend, mixins, extends, components, and install in vue
This article mainly introduces the operations of extend, mixins, extends, components, and install in vue. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it
Preface
Do you know how to use extend, mixins, extends, components, and install?
Do you know their differences?
Do you know their execution order?
You can find these answers below.
1.Vue.extend
1. Use the vue constructor to create a subclass, and the parameter is an object containing component options;
2. It is global
// 创建构造器 var Profile = Vue.extend({ template: '<p>{{extendData}}实例传入的数据为:{{propsExtend}}</p>',//template对应的标签最外层必须只有一个标签 data: function () { return { extendData: '这是extend扩展的数据', } }, props:['propsExtend'] }) // 创建 Profile 实例,并挂载到一个元素上。可以通过propsData传参. new Profile({propsData:{propsExtend:'我是实例传入的数据'}}).$mount('#app-extend')
Conclusion:
Vue.extend actually creates a constructor, the corresponding initialization constructor, and mounts it on the label
github source code, please click here, welcome to star
2.Vue.component
1. Register (name) the extension instance constructor generated by Vue.extend as a global component, and the parameter can be a Vue.extend() extension An instance, or an object (the extend method will be called automatically)
2. Two parameters, a component name, an extend constructor or object
//1.创建组件构造器 var obj = { props: [], template: '<p></p><p>{{extendData}}</p>',//最外层只能有一个大盒子,这个和<tempalte>对应规则一致 data: function () { return { extendData: '这是Vue.component传入Vue.extend注册的组件', } }, }; var Profile = Vue.extend(obj); //2.注册组件方法一:传入Vue.extend扩展过得构造器 Vue.component('component-one', Profile) //2.注册组件方法二:直接传入 Vue.component('component-two', obj) //3.挂载 new Vue({ el: '#app' }); //获取注册的组件 (始终返回构造器) var oneComponent=Vue.component('component-one'); console.log(oneComponent===Profile)//true,返回的Profile构造器</tempalte>
3.mixins
The value can be an array of mix objects, and the mix instance can contain options. The same options will be merged in extend
mixins code:
var mixin={ data:{mixinData:'我是mixin的data'}, created:function(){ console.log('这是mixin的created'); }, methods:{ getSum:function(){ console.log('这是mixin的getSum里面的方法'); } } } var mixinTwo={ data:{mixinData:'我是mixinTwo的data'}, created:function(){ console.log('这是mixinTwo的created'); }, methods:{ getSum:function(){ console.log('这是mixinTwo的getSum里面的方法'); } } } var vm=new Vue({ el:'#app', data:{mixinData:'我是vue实例的data'}, created:function(){ console.log('这是vue实例的created'); }, methods:{ getSum:function(){ console.log('这是vue实例里面getSum的方法'); } }, mixins:[mixin,mixinTwo] }) //打印结果为: 这是mixin的created 这是mixinTwo的created 这是vue实例的created 这是vue实例里面getSum的方法
Conclusion:
1. The order in which mixins are executed is mixins>mixinTwo> ;created (life cycle hook of vue instance);
2. The data attributes in the options, such as data and methods, will be overwritten by the previous ones, and the life cycle hooks will all be executed.
3.extends
The usage of extends is very similar to mixins, except that the parameters received are simple option objects or constructors, so extends can only extend one component at a time
var extend={ data:{extendData:'我是extend的data'}, created:function(){ console.log('这是extend的created'); }, methods:{ getSum:function(){ console.log('这是extend的getSum里面的方法'); } } } var mixin={ data:{mixinData:'我是mixin的data'}, created:function(){ console.log('这是mixin的created'); }, methods:{ getSum:function(){ console.log('这是mixin的getSum里面的方法'); } } } var vm=new Vue({ el:'#app', data:{mixinData:'我是vue实例的data'}, created:function(){ console.log('这是vue实例的created'); }, methods:{ getSum:function(){ console.log('这是vue实例里面getSum的方法'); } }, mixins:[mixin], extends:extend }) //打印结果 这是extend的created 这是mixin的created 这是vue实例的created 这是vue实例的getSum里面的方法
Conclusion:
1.extends execution The order is: extends>mixins>mixinTwo>created
2. The defined attribute coverage rules are consistent with mixins
4.components
Register a local component
//1.创建组件构造器 var obj = { props: [], template: '<p></p><p>{{extendData}}</p>',//最外层只能有一个大盒子,这个和<tempalte>对应规则一致 data: function () { return { extendData: '这是component局部注册的组件', } }, }; var Profile = Vue.extend(obj); //3.挂载 new Vue({ el: '#app', components:{ 'component-one':Profile, 'component-two':obj } });</tempalte>
5 .install
1. It is a plug-in for developing vue. The first parameter of this method is the Vue constructor, and the second parameter is an optional option object (optional)
2.vue. The use method can call the install method, which will automatically prevent multiple registrations of the same plug-in
var MyPlugin = {}; MyPlugin.install = function (Vue, options) { // 2. 添加全局资源,第二个参数传一个值默认是update对应的值 Vue.directive('click', { bind(el, binding, vnode, oldVnode) { //做绑定的准备工作,添加时间监听 console.log('指令my-directive的bind执行啦'); }, inserted: function(el){ //获取绑定的元素 console.log('指令my-directive的inserted执行啦'); }, update: function(){ //根据获得的新值执行对应的更新 //对于初始值也会调用一次 console.log('指令my-directive的update执行啦'); }, componentUpdated: function(){ console.log('指令my-directive的componentUpdated执行啦'); }, unbind: function(){ //做清理操作 //比如移除bind时绑定的事件监听器 console.log('指令my-directive的unbind执行啦'); } }) // 3. 注入组件 Vue.mixin({ created: function () { console.log('注入组件的created被调用啦'); console.log('options的值为',options) } }) // 4. 添加实例方法 Vue.prototype.$myMethod = function (methodOptions) { console.log('实例方法myMethod被调用啦'); } } //调用MyPlugin Vue.use(MyPlugin,{someOption: true }) //3.挂载 new Vue({ el: '#app' });
6. The relationship between each method
Vue.extend and Vue.component are used to create constructors and components ;
mixins and extends are for extending components;
install is for developing plug-ins; The general sequence relationship: Vue.extend>Vue.component>extends>mixins
The above is the entire content of this article, I hope It will be helpful for everyone’s learning. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
For analysis of React components and state|props
For config/index.js in vue :Detailed explanation of configuration
The above is the detailed content of About the operations of extend, mixins, extends, components, and install in vue. For more information, please follow other related articles on the PHP Chinese website!

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.


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

Atom editor mac version download
The most popular open source editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment
