父组件向子组件传递数据(vue.js)
2017-07-03 17:53
736 查看
父组件: app.vue
子组件: header.vue
子组件:
<template>
<div>
<!--
logo 是在data中(父组件)定义的变量
如果需要从父组件中获取logo的值,就需要使用props['logo'], 如30行
在props中添加了元素以后,就不需要在data中(子组件)中再添加变量了
-->
<div id='logo'>{{logo}}(我是父组件传递过来的值)</div>
<ul class="nav">
<li v-for="nav in navs">{{nav.li}}</li>
</ul>
</div>
</template>
<script>
export default{
name:'headerDiv',
data(){
return{
navs:[
{li:'主页'},
{li:'日志'},
{li:'说说'},
{li:'主页'},
{li:'相册'}
]
}
},
props:['logo']
}
</script>
<style scoped>
.nav li{list-style: none;}
</style>
父组件:
<template>
<div id="app">
<img src="./assets/logo.png">
<!--
在调用组件的时候,使用v-bind将logo的值绑定为 APP.vue中定义的变量 logoMsg
然后就能将App.vue中的logoMsg的值传给header.vue 了
-->
<headerDiv :logo="logoMsg"></headerDiv>
<h1>{{msg}}</h1>
<firstcomponent></firstcomponent>
<router-view></router-view>
</div>
</template>
<script>
import firstcomponent from './components/firstcomponent.vue'
import headerDiv from './components/header.vue'
export default {
name: 'app',
data(){
return{
msg:'hellow Vue',
logoMsg:'WiseWrong'
}
},
components:{firstcomponent,headerDiv},
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
子组件: header.vue
子组件:
<template>
<div>
<!--
logo 是在data中(父组件)定义的变量
如果需要从父组件中获取logo的值,就需要使用props['logo'], 如30行
在props中添加了元素以后,就不需要在data中(子组件)中再添加变量了
-->
<div id='logo'>{{logo}}(我是父组件传递过来的值)</div>
<ul class="nav">
<li v-for="nav in navs">{{nav.li}}</li>
</ul>
</div>
</template>
<script>
export default{
name:'headerDiv',
data(){
return{
navs:[
{li:'主页'},
{li:'日志'},
{li:'说说'},
{li:'主页'},
{li:'相册'}
]
}
},
props:['logo']
}
</script>
<style scoped>
.nav li{list-style: none;}
</style>
父组件:
<template>
<div id="app">
<img src="./assets/logo.png">
<!--
在调用组件的时候,使用v-bind将logo的值绑定为 APP.vue中定义的变量 logoMsg
然后就能将App.vue中的logoMsg的值传给header.vue 了
-->
<headerDiv :logo="logoMsg"></headerDiv>
<h1>{{msg}}</h1>
<firstcomponent></firstcomponent>
<router-view></router-view>
</div>
</template>
<script>
import firstcomponent from './components/firstcomponent.vue'
import headerDiv from './components/header.vue'
export default {
name: 'app',
data(){
return{
msg:'hellow Vue',
logoMsg:'WiseWrong'
}
},
components:{firstcomponent,headerDiv},
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
相关文章推荐
- Vuejs——(9)组件——props数据传递
- vue.js组件之间通讯的数据双向绑定----父亲把数据传递给儿子,儿子更改数据后,重新发送给父亲,父亲数据更改后,属性会重新发送个儿子,儿子刷新新数据
- vue.js 组件之间传递数据
- vue.js 学习之组件之间数据传递详解
- vue.js之父组件向子组件传递数据
- vue.js 组件之间传递数据
- vue.js中的子组件与父组件,prop传递数据
- vue.js 组件 之 prop 传递数据
- Vue.js-----轻量高效的MVVM框架(九、组件利用Props传递数据)
- Vue.js-----轻量高效的MVVM框架,组件利用Props传递数据)
- Vue.js-----轻量高效的MVVM框架(九、组件利用Props传递数据)
- Vuejs——(9)组件——props数据传递
- Vue.js实战之组件之间的数据传递
- Vue.js 父子组件相互传递数据
- vue.js 组件之间传递数据
- vue.js组件之间的通讯-----父亲向儿子传递数据,儿子接收父亲的数据
- 关于vue.js父子组件数据传递
- vue.js组件之间传递数据的方法
- Vuejs——(9)组件——props数据传递
- vue.js 组件之间传递数据