How to Create vue components and how to use them, and how to create vue
What is a component?
Component is one of the most powerful functions of Vue. js. Components can expand HTML elements and encapsulate reusable code. At a higher level, a component is a custom element, and the Vue. js compiler adds special features to it. In some cases, components can also be in the form of native HTML elements and can be extended with the is feature.
I know that the core of vue is components, but what are components? What is the use of components?
How to use components? How to create your own components? :
1) Create Your Own Components
Passvue.extend("template")
Use the vue constructor to expand a template, register it, and use it.
<! DOCTYPE html>
Advanced: (the component is embedded in the component, (components under components ))
Through the example below, I want to explain that the component is an extension of the vue constructor, so the component may have almost all attributes of the constructor (before writing this blog, we didn't hear this, so it may be wrong. Don't believe it)
<Body> <div> This is a partial registration </div> <div id = "app1"> <div> <button v-on: click = get> The get method </button> </div> <parent-components> </div> </body> <script> // var child = Vue. extend ({template: "<span> ------ child element ------ </span>"}); var vp = new Vue ({el: '# app1', data :{}, methods: {get: function () {alert ("this is get (global) in the constructor") ;}, components: {"parent-components": {template: "<div> --------- <span> parent components </span> <p> <button v-on: click = get> click get to trigger the parent </button> </p> <div> <child-component> </div> -------- </div> ", components: {// partial registration again "child-component": {template: "<span> ------ child <button v-on: click = get> click the get method in child to trigger </button> element ------ </span> ", methods: {get: function () {alert ("this is a partial registration of get in child-component components") ;}}}, methods: {get: function () {alert ("this is the method used to register parent-components") ;}},},}}); </script>
Do you know? Multiple components can be defined in one components:
Is it low to write html and components? When there are too many templates, isn't it unsightly? Let's use the syntax sugar of the vue component (I don't know why it is called this name)
It is worth reminding you that the data attribute in the component should be defined as a function and an object will be returned,
<Script type = "text/x-template" id = "myComponent"> <div> <span >{{ msg }}</span> </div> </script> <template id = 'mycom'> <span >{{ msg }}</span> </template> <div id = "app"> <parent-component-script> </parent-component-script> <parent-component-tem> </div> var vm = new Vue ({el: "# app", data :{}, components: {"parent-component-script": {template: '# myComponent', data () {return {msg: 'Here is the script' };},}, "parent-component-tem": {template: '# myCom', data () {return {msg: 'Here is the template '}}},},});
You can also be more aggressive: Creation Method
It is worth noting that the property value in props in the component is defined as a camper and will be converted to a hyphen when used.
<div id="app"> <son :son-counter="counter"></son> <p>parent:<input type="text" v-model="counter"/></p></div> const son={ template:`<div>son:<input v-model="sonCounter" /></div>`, props:{sonCounter:Number}, };var app=new Vue({ el:'#app', data:{ counter:0, }, components:{ son } });
Last reminder: The component must be created before the vue is instantiated.
Summary
The above section describes how to create and use vue components. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!