Laravel 5 在vue中调用其他组件的方法
如何在Vue中调用方法其他组件? 我有组件头搜索Laravel 5 在vue中调用其他组件的方法,laravel-5,vue.js,components,vuejs2,vue-component,Laravel 5,Vue.js,Components,Vuejs2,Vue Component,如何在Vue中调用方法其他组件? 我有组件头搜索 <template> <form action=""> <div class="form-group"> <div class="input-group"> <span class="input-group-btn"> <button class="btn" ty
<template>
<form action="">
<div class="form-group">
<div class="input-group">
<span class="input-group-btn">
<button class="btn" type="button">
<i class="fa fa-search"></i>
</button>
</span>
<input type="text" @keyup="search(keyword)" v-model="keyword" class="form-control" placeholder="Search...">
</div>
</div>
</form>
</template>
<script>
export default {
data(){
return { keyword: "" };
},
methods: {
search: function(keyword){
if(keyword == ''){
// Want call method fetchPost in PostHome component here
}else{
}
}
}
}
</script>
导出默认值{
数据(){
返回{关键字:'};
},
方法:{
搜索:函数(关键字){
如果(关键字=“”){
//想在这里调用PostHome组件中的fetchPost方法吗
}否则{
}
}
}
}
我有一个组件PostHome
<template>
<div>
<div class="box r_8 box_shadow" v-for="post in posts">
<div class="box_header">
<a :href="post.url">
<h3 class="mg_bottom_10" v-text="post.title"></h3>
</a>
<small v-text="post.description"></small>
<a :href="post.url" class="box_header_readmore">Read more</a>
</div>
<div class="box_body">
<a :href="post.url" v-show="post.thumbnail">
<img :src="post.thumbnail" class="img_responsive" style="min-height: 300px;background-color: #f1f1f1;
">
</a>
</div>
<div class="box_footer" v-show="post.tags.length > 0">
<ul>
<li v-for="tag in post.tags">
<a v-text="tag.name" href="javascript:void(0)"></a>
</li>
</ul>
</div>
</div>
</div>
</template>
<script>
export default {
data(){
return {
posts: null,
}
},
methods: {
fetchPosts: function(){
var url = base_url + '/user/posts'
this.$http.get(url).then(res => {
this.posts = res.data;
});
}
},
created: function(){
this.fetchPosts();
}
}
</script>
-
导出默认值{
数据(){
返回{
posts:null,
}
},
方法:{
fetchPosts:function(){
var url=base_url+'/user/posts'
这个.http.get(url).then(res=>{
this.posts=res.data;
});
}
},
已创建:函数(){
this.fetchPosts();
}
}
我想当用户键入keyup进行搜索时,如果
关键字==“”
在PostHome组件中调用方法fetchPost方法如果该方法是可重用的,则可以使用mixin
参考:您还可以使用Vuex并发送一个操作,该操作将获取帖子并通过变异将其插入状态。谢谢。我正在使用公共汽车。$emit和公共汽车。$on。它和我一起工作。谢谢。我正在使用总线。$emit和总线。$on。它和我一起工作。