Skip to main content

The use of vue components

 Vue components can be divided into global components and local components. Global components are not used much, but local components are used more. The creation method is to use Vue.extend(options) to create, where options and the options passed in when new Vue(options) are almost the same, but there are also some differences: do not write el, data can no longer be written as an object, and must be written as a function. Generally, partial components are used more in single-page applications. Partial components belong to a Vue instance and are added (mounted) through comopnents. Registering components: local registration: you need to pass in the components option when new Vue, global registration: Vue.component ('component name', component), the component can be defined and placed, or it can be directly in the form of an object. Written in Vue.component.

Here are the steps and methods to actually define the registered usage component:

  <div id="app">
        <school></school>
        <hr>
        <student></student>
        <hello></hello>
    </div>
    //
    <div id="app2">
        <hello></hello>
    </div>
    <div id="app3">
        <app/>
    </div>
    <script>
     Vue.config.productionTip=false
        Vue.component("hello",{
            name:"HHHH",
            template:`
            <div>
            <div>{{helloName}}</div>
            <button @click="show">click</button>
            </div>
            `,
            data(){
                return{
                    helloName:"hellollll"
                }
            },
            methods: {
                show(){
                    alert("hahaha");
                }
            },
        });
        //
        const person={
            template:`
            <div>
                Page:<input v-model="personAge"/>
                </div>
            `,
            data(){
                return{
                    personAge:100
                }
            }
        };
        //
        const school=Vue.extend({
            template:`<div>
                <p>{{schoolName}}</p>
                <p>{{schoolAddr}}</p>
                </div>`,
            data(){
                return{
                    schoolName:"helloSChool",
                    schoolAddr:"beijing"
                }
            }
        });
        const student=Vue.extend({
            template:`<div>
                <p>{{studentName}}</p>
                <p>{{studentAge}}</p>
                <person></person>
                // <hello></hello>
                </div>`,
            data(){
                return{
                    studentName:"aaa",
                    studentAge:18
                }
            },
            components:{
                person:person,
            }
        });
        // console.log(student instanceof VueComponent);
        // console.log(student.__proto__ instanceof Vue);
        // console.log(student.__proto__);


//
        const app={
            template:`
            <div>
                <hello></hello>
                <school></school>
                <student></student>
                </div>`,
                components:{
                    school:school,
                    student:student
                }
        }

        var vm = new Vue({
            el: '#app',
            // data: {},
            // methods: {},
            components:{
                school:school,
                student:student
            }
        });
        // console.log(student instanceof vm);
        console.log(student.__proto__===vm.prototype);//false
        console.log(student.__proto__.__proto__===vm.__proto__);//false
        console.log(student.__proto__===Vue);//false
        console.log(student.prototype.__proto__===Vue.prototype);//true

        new Vue({
            el:"#app2",
        })
        new Vue({
            el:"#app3",
            components:{
                app:app
            }
        });
    </script>

Comments

Popular posts from this blog

What is the difference between width property 100% and auto in css?

 width:auto: The default width value of block-level elements. When set to this value, the browser will automatically select an appropriate width value to adapt to the width of the parent element. When the width is set to 100%, the width of the child element box The value is equal to the parent's content, and as the parent's content automatically changes, after adding the padding and margin of the child element, its width remains unchanged, which is the difference from setting it to auto. But we most often use width:auto, because it is more flexible, width:100% is used less, because when adding padding or margin, this method is easy to make it exceed the parent box and destroy the original layout.

Access to XMLHttpRequest at 'http://localhost:8080/xxx' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 Due to browser same-origin policy restrictions. The Same Origin Policy is a convention. This is the main and essential security feature of the browser. Without the same-origin policy, normal browser functionality may be affected. The web is built on the same-origin policy, and browsers are just one implementation of the same-origin policy. The Same Origin Policy prevents JavaScript scripts from one domain from interacting with content from another domain. The so-called same origin (that is, the same domain) means that two pages have the same protocol, host and port.