user.vue
<template>
<view>
<div class="hello">
<p>{{subTitle}}</p>
<ul>
<li v-for="user in users" @click="choose(user)">{{user}}</li><!--
</ul>
</div>
</view>
</template>
<script>
export default {
props:{
users:{
type:Array,
required:true
}
},
data(){return {}},
methods:{
choose(data){this.$emit("onChoose",data)}
},
inject: ['subTitle'],
mounted () {
console.log(this.subTitle);
}
}
</script>
<style>
.hello{background-color: #C0C0C0;}
.hello ul li {cursor: pointer;}
</style>
parent.vue
<template>
<view>
<h1>你的名字:<span>{{name}}</span></h1>
<users v-bind:users="arr" v-on:onChoose="changeVal"></users>
</view>
</template>
<script>
import Users from './users.vue'
export default {
name:"parent",
components:{Users},
data() {
return {
name:"",
arr:["Henry","Bucky","Emily"],
};
}
,methods:{
changeVal(data){
console.log('你的选择:'+data);
this.name=data;
}
}
, provide: {
subTitle: '请选择'
}
}
</script>
<style>
h1 span{color: red;}
</style>