美文网首页
Vue中组件

Vue中组件

作者: 张鑫冲 | 来源:发表于2018-09-20 15:04 被阅读0次

1,组件(component): 是vue最强大的功能之一 组件化开发 组件可以扩展 HTML 元素,封装可重用的代码。全局 局部

2,组件之间的传值

父传子 用属性传

子传父 用事件传

同级之间传值

1父传子:

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>Document</title>
    </head>

    <body>
        <script src="js/vue.js"></script>
        <div id="box">
            <one></one>
        </div>
        <script>
            Vue.component("one", {
                template: `
                <div>
            <zhang v-bind:xin='arrs'></zhang>
            <chong v-bind:xin1='arr'></chong>
            </div>
            `,
                data: function() {
                    return {
                        arr: ['苹果', '橘子', '葡萄'],
                        arrs: '水果列表'
                    }
                }
            })
            Vue.component('zhang', {
                props: ['xin'],
                template: `
            <h2>{{xin}}</h2>
            `
            })
            Vue.component('chong', {
                props: ['xin1'],
                template: `
            <ul>
            <li v-for='value in xin1'>{{value}}</li>
            </ul>
            `
            })
            new Vue({
                el: '#box'
            })
        </script>
    </body>

</html>

2父传子做的一个添加删除效果:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <script src="js/vue.js"></script>
        <div id="box">
            <zhang></zhang>
        </div>
        <script>
            Vue.component('zhang',{
                template:`
                <div>
                <input v-model='arrs'></input>
                <button v-on:click='add'>添加</button>
                <zhang1 v-bind:xin='arr'></zhang1>
                </div>
                `,
                data:function(){
                    return{
                        arr:['苹果','西瓜','香蕉'],
                        arrs:''
                    }
                },
                methods:{
                    add:function(){
                        this.arr.push(this.arrs),
                        this.arrs=''
                    }
                }
            })
            
            Vue.component('zhang1',{
                props:['xin']
                ,template:`
                <ul>
                <li v-for='(value,index) in xin'>{{value}}
                <button v-on:click='add(index)'>删除</button>
                </li>
                 </ul>  
                `,
                methods:{
                    add:function(ind){
                        this.xin.splice(ind,1)
                    }
                }
            })
            new Vue({
                el:'#box'
            })
        </script>
    </body>
</html>

3,父传子做的一个购物车效果:

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>

    <body>
        <script src="js/vue.js"></script>
        <div id="box">

            <zhang></zhang>
        </div>
        <script>
            Vue.component('zhang', {
                template: `
                <table border="1" cellspacing="0" cellpadding="" style="width: 500px; margin: 0 auto; text-align: center;">
                <tr>
                <th>编号</th>
                    <th>名称</th>
                    <th>价格</th>
                    <th>数量</th>
                    <th>小计</th>
                </tr>
                <zhang1 v-bind:xin='arr'></zhang1>
            </table>
            
                `,
                data: function() {
                    return {
                        arr: [{
                                name1: '苹果',
                                name2: 2,
                                name3: 2,
                                name4: 4
                            },
                            {
                                name1: '西瓜',
                                name2: 3,
                                name3: 3,
                                name4: 9
                            },
                            {
                                name1: '葡萄',
                                name2: 4,
                                name3: 4,
                                name4: 16
                            }
                        ]
                    }
                }
            })

            Vue.component('zhang1', {
                props: ['xin'],
                template: `
                <tbody>
                <tr v-for="(value,index) in xin">
                <td>{{index+1}}</td>
                <td>{{value.name1}}</td>
                <td>{{value.name2}}</td>
                <td>
                <button v-on:click='add(index)'>+</button>
                {{value.name3}}
                <button v-on:click='app(index)'>-</button>
                </td>
                <td>{{value.name4}}</td>
                </tr>
                <tr>
                <td colspan=5>总价:{{sum}}</td>
                </tr>
                </tbody>
                `,
                data: function() {
                    return {
                        sum: 0
                    }
                },
                methods: {
                    add: function(ind) {
                        this.xin[ind].name3++;
                        this.xin[ind].name4 = this.xin[ind].name3 * this.xin[ind].name2;
                        this.apd()
                    },
                    app: function(ind) {
                        if(this.xin[ind].name3 > 1) {
                            this.xin[ind].name3--
                        }
                        this.xin[ind].name4 = this.xin[ind].name3 * this.xin[ind].name2
                        this.apd()
                    },
                    apd: function() {
                        for(var i = 0, zong=0; i < this.xin.length; i++) {
                            zong+=this.xin[i].name4
                        }
                        this.sum=zong;
                    }
                }
            })
            new Vue({
                el: '#box'
            })
        </script>
    </body>
</html>
子传父:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
   <div id='app'>
       <my-father></my-father>
   </div>
    <script src='js/vue.js'></script>
    <script>
        Vue.component('my-father',{
            template:`
                <div>
                   <h1>{{mess}}</h1>
                   <my-child @add='app'></my-child>
                </div>
             `,
            data:function(){
                return{
                    mess:''
                }
            },
            methods:{
               app:function(txt){
                   this.mess=txt
               }
             }
        })
        
        Vue.component('my-child',{
            template:`
                 <button @click='sendFather'>给父组件</button>
             `,
            data:function(){
                return{
                    msg:'hello Vue'
                }
            },
            methods:{
                sendFather:function(){
//                   this.$emit('自定义事件',参数) 
                    this.$emit('add',this.msg)
                }
            }
        })
        
        
        new Vue({
            el:"#app"
        })
    </script>
</body>
</html>

相关文章

  • vue学习

    vue核心是组件,组件是vue实例,没有el,其他data,computed等等方法都有。组件定义在vue中,用在...

  • vue父子组件传递数据方法

    标签(空格分隔): vue 父组件向子组件传递数据 Vue中父组件向子组件传递数据用props 举个例子 父组件中...

  • vue父子组件间调用方法

    标签(空格分隔): vue 子组件中 父组件中

  • vue 的坑

    记录vue中遇到的坑(不管是组件ele组件还是vue本身)Element 组件中的bug 寻找地址:https:/...

  • Vite按需引入自定义组件unplugin-vue-compon

    unplugin-vue-components插件可以在Vue文件中自动引入组件(包括项目自身的组件和各种组件库中...

  • Vue.js的组件化思想 —上

    一、Vue中的组件 Vue视图层的灵魂 — 组件化 组件(Component)是 Vue.js 最强大的功能之一...

  • vue中的slot插槽的用法

    App.vue作为b.vue的父组件引用子组件 b.vue中的写法

  • 组件的局部注册

    组件和组件名 在Vue实例中通过Vue.component({/.../})绑定的组件都自动成为全局组件,不管这个...

  • Vue中多个元素或组件的过渡

    Vue中多个元素的过渡 Vue中多个组件的过渡(通过动态组件实现组件的过渡动画效果)image.png

  • Vue/组件

    Vue/组件 创建组件 单独声明一个Vue.component,使用只需要在Vue实例下使用定义的组件名在组件中d...

网友评论

      本文标题:Vue中组件

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