JavaScript/vue.js/调用其他组件的函数:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
第1行: 第1行:
== 引言 ==
== 引言 ==
在被调用的方法所在的文件中,使用事件车注册事件。
在被调用的方法所在的文件中,使用事件车注册事件。
{{hc|bus.js|<syntaxhighlight lang="javascript">
/*!
* @auther kangkk
* @desc 事件车
*/
import Vue from 'vue'
export default new Vue()</syntaxhighlight>}}


{{hc|listLeft.vue|<syntaxhighlight lang="html">
{{hc|listLeft.vue|<syntaxhighlight lang="html">
<script>
<script>
import EventBus from '@@/plugins/bus.js'
mounted(){
mounted(){
   const _self = this;
   const _self = this;
第17行: 第26行:
{{hc|searchmodule.vue|<syntaxhighlight lang="html">
{{hc|searchmodule.vue|<syntaxhighlight lang="html">
<script>
<script>
import EventBus from '@@/plugins/bus.js'
inputValueChange () {
inputValueChange () {
   if(this.$route.path === '/search') {
   if(this.$route.path === '/search') {

2020年7月28日 (二) 07:00的版本

引言

在被调用的方法所在的文件中,使用事件车注册事件。

bus.js
/*!
 * @auther kangkk
 * @desc 事件车
 */
import Vue from 'vue'
export default new Vue()
listLeft.vue
<script>
import EventBus from '@@/plugins/bus.js'
mounted(){
  const _self = this;
  EventBus.$on('searchInputChange', function (searchValue) {
    _self.renderRedirectSearch(searchValue);
  })
},
</script>

在另一个页面类似如下调用 listLeft.vue 组件中的方法

searchmodule.vue
<script>
import EventBus from '@@/plugins/bus.js'
inputValueChange () {
  if(this.$route.path === '/search') {
    EventBus.$emit('searchInputChange', this.searchValue);
  }
}
</script>