当前位置: 首页 > news >正文

企业英文网站58网络推广

企业英文网站,58网络推广,wordpress发布文章关键词,廊坊哪里有做网站建设的1、监听自定义事件 父组件通过使用 Prop 为子组件传递数据,但如果子组件要把数据传递回去,就需要使用自定义事件来实现。父组件可以通过 v-on 指令(简写形式“”)监听子组件实例的自定义事件,而子组件可以通过调用内建…

1、监听自定义事件

父组件通过使用 Prop 为子组件传递数据,但如果子组件要把数据传递回去,就需要使用自定义事件来实现。父组件可以通过 v-on 指令(简写形式“@”)监听子组件实例的自定义事件,而子组件可以通过调用内建的 $emit() 方法并传入事件名称来触发自定义事件。

组件之间的数据传递:

父传子:使用 Props 属性。

子传父:使用 $emit() 方法。

$emit() 方法的语法格式如下:

this.$emit( eventName, […args] )

参数说明:

eventName:传入事件的名称。

 […args]:触发事件传递的参数,该参数是非必选。

【实例】使用$emit()方法,实现子组件向父组件传递事件。

(1)创建 ParentComponent.vue 父组件

<template><fieldset><legend>父组件</legend><h3>父组件接收到子组件传递的数据:</h3><p>博客信息:{{ blogName }}</p><p>博客信息:{{ blogUrl }}</p><!-- 第三步:使用组件 --><ChildComponent @receiverData="getBlogInfo" /></fieldset>
</template><script>
//第一步:引用组件
import ChildComponent from '@/components/ChildComponent.vue'export default {data() {return {blogName: '',blogUrl: ''}},//第二步:注册组件components: {ChildComponent,},//方法methods: {getBlogInfo: function (blogName, blogUrl) {this.blogName = blogName;this.blogUrl = blogUrl;}}
}
</script>

(2)创建 ChildComponent.vue 子组件

<template><fieldset><legend>子组件</legend><button @click="sendData">传递数据给父组件</button></fieldset>
</template><script>
export default {data() {return {blogName: '您好,欢迎访问 pan_junbiao的博客',blogUrl: 'https://blog.csdn.net/pan_junbiao'}},methods: {sendData: function () {// 核心代码:使用 $emit() 方法this.$emit('receiverData', this.blogName, this.blogUrl);}}
}
</script>

(3)在 App.vue 根组件中,引入父组件

<template><!-- 第三步:使用组件 --><ParentComponent />
</template><script>
//第一步:引用组件
import ParentComponent from '@/components/ParentComponent.vue'export default {//第二步:注册组件components: {ParentComponent,}
}
</script><style></style>

执行结果:

2、组件事件配合 v-model 指令

如果是在子组件中用户输入数据,我们希望在获取数据的同时发生数据给父组件,这是可以配合 v-model 指令使用。

【实例】子组件中用户输入数据,在父组件中实时获取数据。

(1)修改 ParentComponent.vue 父组件

<template><fieldset><legend>父组件</legend><!-- 第三步:使用组件 --><ChildComponent @searchEvent="getSearch" /><h3>父组件接收到子组件传递的数据:</h3>接收到的搜索关键字:<input type="text" v-model="search" /></fieldset>
</template><script>
//第一步:引用组件
import ChildComponent from '@/components/ChildComponent.vue'export default {data() {return {search: ''}},//第二步:注册组件components: {ChildComponent,},//方法methods: {getSearch: function (keyword) {this.search = keyword;}}
}
</script><style>
input {width: 300px;padding: 3px;font-size: 16px;
}
</style>

(2)修改 ChildComponent.vue 子组件

<template><fieldset><legend>子组件</legend>搜索:<input type="text" v-model="search" /></fieldset>
</template><script>
export default {data() {return {search: ''}},// 监听器watch: {search(newValue, oldValue) {// 核心代码:使用 $emit() 方法this.$emit('searchEvent', newValue);}}
}
</script>

执行结果:

http://www.tj-hxxt.cn/news/63411.html

相关文章:

  • 网站more应该怎么做快速优化网站排名的方法
  • 广东高端网站建设百度用户服务中心人工24小时电话
  • 金泉网做网站多少钱如何做网络推广赚钱
  • 企业网站制作公司电话百度网站排名seo
  • wordpress网站相册seo快速优化报价
  • joomla 和 wordpress泰安seo培训
  • 支付宝网站申请接口如何建立自己的网页
  • 做网站注册会员加入实名认证功能排名优化公司口碑哪家好
  • 做idc销售怎样建网站淘宝流量平台
  • 官方网站建设对比站长之家音效
  • 网站店铺建设杭州最好的电商培训机构
  • 公司做网站之前要准备什么软件太原网站快速排名提升
  • 音乐培训如何做网站宣传网络服务商主要包括
  • 贵阳市建设管理信息网站seo推广哪家公司好
  • 网页升级访问中未满十八岁百度seo排名规则
  • php和什么语言做网站哪里有seo排名优化
  • 十大网购平台有哪些北京aso优化
  • 有哪些网站做的好处优化seo哪家好
  • 昆明网站建设服务seo推广小分享
  • 如何做网站性能优化什么叫外链
  • 新乡河南网站建设百度宣传推广
  • 做网站茶叶首页标题怎么写百度站长平台论坛
  • 阿里云网站建设和部署框架成都营销推广公司
  • 网上做任务赚钱网站有哪些semantic scholar
  • 做网站去哪推广好阿里云空间+1对1私人专属设计师
  • 临河网站建设seo详细教程
  • 网站做视频厦门seo排名公司
  • 一台主机做两个网站网络营销策划书1000字
  • 做网站需要知道什么软件国家免费职业技能培训官网
  • 各行各业网站建设公司网站建设教程