vm.$props

props—父类向子类传递信息,子类接受并返回结果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="vue.js"></script>
</head>
<body>
    <div id="app">
        <my-parent></my-parent>
    </div>
  <template id="parent">
        <div>
            <h3>手机信息搜索</h3>
            手机品牌:<input type="text" v-model='brand'>
            <my-child v-bind:name='brand'></my-child>
        </div>
    </template>
 <template id="child">
        <ul>
            <li>手机品牌:{{show.brand}}</li>
            <li>手机型号:{{show.type}}</li>
            <li>市场价格:{{show.price}}</li>
        </ul>
    </template>
 <script>
        Vue.component('my-parent',{
            data () {
                return {
                brand: ''
            }
        },
            template:"#parent"
        })
Vue.component('my-child',{
        template:"#child",
        data(){
            return{
                content:[
                    {brand: '华为', type: 'Mate20', price: 3699},
                    {brand: '苹果', type: 'iPhone7', price: 2949},
                    {brand: '三星', type: 'Galaxy S8+', price: 3299},
                    {brand: 'vivo', type: 'Z5x', price: 1698},
                    {brand: '一加', type: 'OnePlus7', price: 2999},
                    {brand: '360', type: 'N7 Pro', price: 1099},
                    {brand: 'oppo', type: 'Reno', price: 2599}
                ],
                show:{brand:'',type:'',price:''}
            }
        },
        props:['name'],
        watch:{
            name(){
                if(this.$props.name){
                    var found = false;
                    this.content.forEach((value,index)=>{
                        if(value.brand===this.$props.name){
                            found = value;
                        }
                    }),
                    this.show=found?found:{brand:'',type:'',price:''}
                }
            }
        }
    })
     var vm = new Vue({
            el:'#app',
            data:{
                
            }
        })
    </script>
</body>
</html>

在这里插入图片描述


(现在还是有点似懂非懂,欢迎大佬详细解惑—数据属性传递)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值