美文网首页
Vue组件和父子组件

Vue组件和父子组件

作者: GaoEnron | 来源:发表于2020-01-01 21:28 被阅读0次

【一】Vue组件化

  1. 创建组件构造器对象

    • Vue.extend() 创建组件构造器

      const cpnc = Vue.extend({
                 template: `
                 <div>
                     <h2>我是标题</h2>
                     <p>我是内容</p>
                     <p>我是内容</p>
                 </div>
                 `
      })
      
  1. 注册我们的组件

    Vue.component('my-cpn', cpnc);
    
  2. 使用组件

【二】局部组件创建

    const cpnc = Vue.extend({
            template: `
            <div>
                <h2>我是标题</h2>
                <p>我是内容</p>
                <p>我是内容</p>
            </div>
            `
        })
        /* 注册组件的过程 */
        Vue.component('my-cpn', cpnc);
            
            
        
            
        const app = new Vue({
                el:"#app3",
                data: {
                    message: "我们测试"
                },
                methods: {
                    valueChange: function(event) {
                        this.message = event.target.value;
                        console.log('打印方法')
                    }
                },
                /* 注册基础组件 */
                components: {
                    cpn: cnnc;
                }
            })
        </script>

【三】父组件和子组件

在某一个注册组件中注册另一个子组件

// 子组件
const cpnc1 = Vue.extend({
            template: `
            <div>
                <h2>我是标题</h2>
                <p>我是内容</p>
                <p>我是内容</p>
            </div>
            `
        })
        // 是我们父组件
        const cpnc2 = Vue.extend({
            template: `
            <div>
                <h2>我是标题2</h2>
                <p>我是内容2</p>
                <p>我是内容2</p>
            </div>
            `
            /* 子组件属性 */
            comments: {
                cpn1: cpnc1
            }
})

相关文章

网友评论

      本文标题:Vue组件和父子组件

      本文链接:https://www.haomeiwen.com/subject/qzbgoctx.html