props 父子传值 简单的路由传参

  • Post author:
  • Post category:其他


1.父给子 tt(父) son(子)

tt
<template>
    <div>
        付给子
        <Son :msg='msg'></Son>
    </div>
</template>

<script>
    import Son from './Son'
    export default {
        name: "User",
        components: {Son},
        data(){
            return{
                msg:'这是数据'
            }
        },
    }
</script>

son
<template>
    <div>
        父给子
        {{msg}}
    </div>
</template>

<script>
    export default {
        name: "Son",
        props:{
            msg:String
        }
    }
</script>

2. 子给付

tt
<template>
    <div>
        子给{{msg}}父
        <Son @gei="sou"></Son>
    </div>
</template>
<script>
    import Son from './Son'
    export default {
        name: "User",
        components: {Son},
        data(){
            return{
                msg:'',
            }
        },
        methods:{
            sou(msg){
                this.msg=msg;
            }
        }
    }
</script>

son
<template>
    <div>
        子给父
        <input type="text" v-model="xxx"></input>
    </div>
</template>
<script>
    export default {
        name: "Son",
        data(){
            return{
                xxx:''
            }
        },
        watch:{
            xxx(){
                this.$emit("gei",this.xxx);
            }
        }
    }
</script>

3.路由传参

tt
<template>
    <div>
        <h1>page1</h1>
        <router-link :to="{name:'son',params:{'id':this .id}}">son</router-link>
        <br>
        <p>{{msg}}</p><br>
        <h1> 欢迎回来。{{this.$route.params.num}}</h1>
    </div>
</template>
<script>
    export default {
        data () {
            return {
                msg: "我是page1组件",
                id:1
            }
        }
    }
</script>


son

<template>
    <div>
        <h1>page2</h1>
        <router-link :to="{name:'tt',params:{'num':this.num}}">tttttt</router-link>
        <br>
        <p>{{msg}}</p><br>
        <h1> 欢迎回来。{{this.$route.params.id}}</h1>
    </div>
</template>
<script>
    export default {
        data () {
            return {
                msg: "我是page2组件",
                num:2
            }
        }
    }
</script>


 { path: '/tt', name: 'tt', component: () => import('../views/service/tt.vue')},



版权声明:本文为qq_43658378原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。