JavaScript/vue.js/调用其他组件的函数:修订间差异
来自康健生活
< JavaScript | vue.js
创建页面,内容为“== 引言 == 在被调用的方法所在的文件中,使用事件车注册事件。 {{hc|listLeft.vue|<syntaxhighlight lang="html"> <script> </script> </syntaxhi…” |
|||
| (未显示同一用户的2个中间版本) | |||
| 第1行: | 第1行: | ||
== 引言 == | == 引言 == | ||
先在 plugins 文件夹下新建 bus.js 文件实现事件车 | |||
{{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(){ | |||
const _self = this; | |||
EventBus.$on('searchInputChange', function (searchValue) { | |||
_self.renderRedirectSearch(searchValue); | |||
}) | |||
}, | |||
</script> | |||
</syntaxhighlight>}} | |||
在另一个页面类似如下调用 listLeft.vue 组件中的方法 | |||
{{hc|searchmodule.vue|<syntaxhighlight lang="html"> | |||
<script> | |||
import EventBus from '@@/plugins/bus.js' | |||
inputValueChange () { | |||
if(this.$route.path === '/search') { | |||
EventBus.$emit('searchInputChange', this.searchValue); | |||
} | |||
} | |||
</script> | </script> | ||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
2020年7月28日 (二) 07:01的最新版本
引言
先在 plugins 文件夹下新建 bus.js 文件实现事件车
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>
No categories