页面中
<template>
<div>
<item @gochild="to" v-for="(theStr,index) in arr ":key="index" :str="theStr"></item>
</div>
</template>
<script>
import Item from "../components/Item";
export default {
name: "Shop",
data(){
return{
arr:['san','si','wu','liu']
}
},
components:{
Item
},
methods:{
to(arg) {
console.log(arg)
}
}
}
</script>
<style scoped>
</style>
2.组件中
<template>
<div>
<span @click="btn(str)">{{str}}</span>
</div>
</template>
<script>
export default {
name: "Item",
props:{
str:String
},
methods:{
btn(theStr){
this.$emit('gochild',theStr)
}
}
}
</script>
<style scoped>
</style>
第二种组件向外传参
<template>
<div>
<item :theFn="to" v-for="(theStr,index) in arr ":key="index" :str="theStr"></item>
</div>
</template>
<script>
import Item from "../components/Item";
export default {
name: "Shop",
data(){
return{
arr:['san','si','wu','liu']
}
},
components:{
Item
},
methods:{
to(arg) {
console.log(arg)
}
}
}
</script>
<style scoped>
</style>
<template>
<div>
<span @click="btn(str)">{{str}}</span>
</div>
</template>
<script>
export default {
name: "Item",
props:{
str:String,
theFn:Function
},
methods:{
btn(theStr){
this.theFn(theStr)
}
}
}
</script>
<style scoped>
</style>