Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

vue组件中props有什么作用

<div id="app">
    <ul>
        <todo-item v-for= "item in groceryList" v-bind:todo="item"></todo-item>    
    </ul>
</div>

Vue.component('todo-item',{
    props:['todo'],//这段
    template:"<li>{{ todo.text}}</li>"
})
var app = new Vue({
    el:'#app',
    data:{
        groceryList:[
            {text:'shucai'},
            {text:'nairao'},
            {text:'else'}
        ]
    }
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Vue.component('todo-item',{
    props:['todo'],//这段
    template:"<li>{{ todo.text}}</li>"
})

正如上面代码所写,你在组件的模板中写上了{{todo.text}},可是todo并没有text这个键
是在父组件中定义的

<div id="app">
    <ul>
        <todo-item v-for= "item in groceryList" v-bind:todo="item"></todo-item>    
    </ul>
</div>

在父组件中你绑定了todo=item,而item中有text这个键
综上所述,其实就是父组件向子组件传递参数的作用,有一句官方上的话是,props down ,events up.希望对你有帮助哦~


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...