之前写过uniapp如何封装组件及在页面中如何引入。今天遇到了在组件中有点击事件,在页面中如何引入组件及事件。本文主要是做笔记记录下,下面直接看代码:
子页面
<template name="titleBox">
<view class="p_title">
<text>{{p_title}}</text>
<text @click="more()" class="p_more">{{p_more}}</text>
</view>
</template>
<script>
export default{
name:"titleBox",
props:{
p_title:{
type: String,
default: ''
},
p_more:{
type: String,
default: ''
}
},
data(){
return{
}
},
methods:{
more() {
console.log('我是子组件')
this.$emit('moreBtn',this.p_more) //(父组件中触发的事件名,要传的变量名)
}
}
}
</script>
<style lang="scss">
.p_title{
font-size: $uni-font-size-lg;
color: $uni-color-error;
padding-bottom:$uni-spacing-row-base;
text{
width: 70%;
display: inline-block;
font-weight: bold;
}
.p_more{
font-size: $uni-font-size-sm;
color: $uni-text-color-placeholder;
width: 30%;
text-align: right;
font-weight: normal;
}
}
</style>
父页面引入:
<titleBox :p_title="publicTitle" :p_more="publicMore" @moreBtn="publicmoreUrl"></titleBox>
publicTitle:'限时抢购',
publicMore:'逛一逛',
publicmoreUrl(e){
uni.navigateTo({
url:"/pages/details/index",
});
},