更新-----------
1 父組件向子組件傳值:通過props數組:
在vue-cli Login.vue父組件中有AcceptAndRefuse.vue子組件,首先import進子組件hello,然后在components中注冊組件,然后在template中使用<hello></hello>組件,制定msg信息
//父組件 App.vue
<template>
<div id="app">
<!-- the router outlet, where all matched components would ber viewed -->
<router-link v-bind:to="'/'">Home</router-link>
<!-- 為我們創建兩個錨點標簽,並動態路由,使頁面不需要重新加載-->
<router-link v-bind:to="'/about'">About</router-link>
<router-view></router-view>
<hello msg-father="dad無可奉告"></hello>
</div>
</template>
<script>
import hello from './components/hello'
export default {
name: 'app',
components:{
hello
}
}
</script>
<!-- styling for the component -->
<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>
//子組件./components/Hello.vue
//通過props接收信息
<template>
<div class="hello">
<p>{{msgFather}}</p>
</div>
</template>
<script>
export default {
name: 'hello',
data () {
return {
}
},
props:['msgFather']
}
</script>
<style scoped>
</style>
子組件即可收到通信
傳進來的數據是mes-father, vue轉化成mesFather, 在js 里面寫mesFather
http://www.jianshu.com/p/2670ca096cf8
2 子組件向父組件傳值:自定義事件,this.$emit,發送信息,在父組件中
子組件:
<template>
<div class="hello">
<!-- 添加一個input輸入框 添加keypress事件-->
<input type="text" v-model="inputValue" @keypress.enter="enter">
<p>{{mes}}</p>
</div>
</template>
<script>
export default {
props:['mes'],
// 添加data, 用戶輸入綁定到inputValue變量,從而獲取用戶輸入
data: function () {
return {
inputValue: ''
}
},
methods: {
enter () {
this.$emit("sendiptVal", this.inputValue)
//子組件發射自定義事件sendiptVal 並攜帶要傳遞給父組件的值,
// 如果要傳遞給父組件很多值,這些值要作為參數依次列出 如 this.$emit('valueUp', this.inputValue, this.mesFather);
}
}
}
</script>
父組件:
<template> <div>
<p> father</p>
<accept-and-refuse :mes=loginJson.animal @sendiptVal='showChildMsg'></accept-and-refuse>
</div> </template> <script> import AcceptAndRefuse from '@/components/public/AcceptAndRefuse' export default { data() { return { message:'hello message', loginJson:{ "animal":"dog" } }, mounted(){ }, methods: { components:{ AcceptAndRefuse } } </script> <style> </style>
在vue組件中,import子組件,components中注冊,template中使用,router跳轉
