美文网首页
vue父子通信(provider,inject)方法

vue父子通信(provider,inject)方法

作者: 追马的时间种草 | 来源:发表于2019-12-25 16:12 被阅读0次

直接上代码吧,效果同vue通信事例(发布订阅模式)的效果一样

<vote-button :eventbus='eventBus' @changeallnum='changeAllNum'></vote-button>
<!DOCTYPE html>
<html lang="en">

<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>
    <link rel="stylesheet" href="./node_modules/bootstrap/dist/css/bootstrap.min.css">
   <style>
        .container {
            box-sizing: border-box;
            padding: 10px 20px;
            margin: 0 30px;
            width: 300px;
            border: 1px solid #aaaaaa;
        }

        .container h3 {
            font-size: 16px;
            line-height: 32px;
            border-bottom: 1px dashed #dedede;
        }
    </style>
</head>

<body>
    <div id="app">
        <my-vote v-for='(item,index) in voteList' :key='item.id' :post='item'></my-vote>
    </div>
    <!-- TEMPLATE -->

    <!-- all`s parent -->
    <template id="MyVoteTemplate">
        <div class="container">
            <h3><span v-text='post.title'></span><span>总票数:</span><span v-text='nums.supNum+nums.oppNum'></span></h3>
            <vote-content></vote-content>
            <vote-button></vote-button>
        </div>
    </template>

    <!-- Content: vote show -->
    <template id="VoteCotentTemplate">
        <div class="content">
            <p>支持票数:<span v-text='nums.supNum'></span></p>
            <p>反对票数:<span v-text='nums.oppNum'></span></p>
            <p>支持率:<span v-text='ratio'></span></p>
        </div>
    </template>

    <!-- Button:support or oppose -->
    <template id="VoteButtonTemplate">
        <div class="footer">
            <button type="button" class="btn btn-success" @click="handle('support')">support</button>
            <button type="button" class="btn btn-danger" @click="handle('oppose')">oppose</button>
        </div>
    </template>
    <script src="./node_modules/vue/dist/vue.js"></script>
    <script src="./node_modules/element-ui/lib/index.js"></script>
    <script>
        const VoteContent = {
            template: '#VoteCotentTemplate',
            inject: ['nums'],
            computed: { //计算比例
                ratio() {
                    let total = this.nums.supNum + this.nums.oppNum
                    if (total === 0) {
                        return '0%'
                    }
                    return (this.nums.supNum / total * 100).toFixed(2) + '%'
                }
            }
        }
        const VoteButton = {
            template: '#VoteButtonTemplate',
            inject: ['change'],
            methods: {
                handle(type) {
                    this.change(type)
                }
            }
        }
        const MyVote = {
            template: '#MyVoteTemplate',
            props: ['post'],
            provide() {
                return {
                    nums:this.nums,
                    change: this.change
                }
            },
            data() {
                return {
                    nums: {
                        supNum: 0,
                        oppNum: 0
                    }
                }
            },
            components: {//将子组件在父组件中进行注册
                VoteButton,
                VoteContent
            },
            created() {
                console.log(this._provided)
            },
            methods: {
                change(type) {
                    type === 'support' ? this.nums.supNum++ : this.nums.oppNum++
                }
            }
        }
        let vm = new Vue({
            el: '#app',
            data: {
                voteList: [{
                    id: 1,
                    title: '晓林很帅'
                },
                {
                    id: 2,
                    title: '晓林很好'
                }]
            },
            //=>注册当前组件(视图中)需要的局部组件
            components: {
                MyVote
            },
        })
    </script>
</body>
</html>

相关文章

  • vue父子通信(provider,inject)方法

    直接上代码吧,效果同vue通信事例(发布订阅模式)的效果一样

  • vue组件通信之provider/inject

    provider/inject:简单的来说就是在父组件中通过provider来提供变量,然后在子组件中通过inje...

  • vue组件间通信的一些实用方法(VUE2.x)

    vue组件间通信的一些实用方法(VUE2.x) 一、父子组件间通信 常用的父子组件通信方法,一般涉及props和$...

  • vue 中父子组件通信 js 引用的作用

    父子组件之间的通信 关于父子之间的通信问题,最常用的就是 props 和 provide/inject,子组件使用...

  • Vue相关知识点

    1、vue父子组件之间的通信 在vue组件通信中其中最常见通信方式就是父子组件之中的通性,而父子组件的设定方式在不...

  • 组件通信

    了解自定义事件,listener的应用 理解子父通信、非父子通信、provide/inject 的应用场景 掌握子...

  • 09-生命周期及非父子组件间的通信

    一. Vue生命周期 二、生命周期代码 三、非父子组件通信 vue中非父子组件通信需要借助一个空的vue实例,案...

  • vue事件总线EventBus

    vue组件有父子组件通信:props兄弟组件通信:可以使用vuex,还可以使用事件总线eventBus 使用方法:...

  • Vue如何实现组件通信?

    Vue组件通信的三种情况: 父子通信 爷孙通信 兄弟通信 父子通信:父组件使用Prop向子组件传递数据,子组件通过...

  • Vue 组件间通信

    背景 在使用 Vue 时,经常会有父子组件之间的通信需求,有很多种方法可以实现。 实现 父组件向子组件通信 方法一...

网友评论

      本文标题:vue父子通信(provider,inject)方法

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