作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的观点是这样的:
<div class="row">
<div class="col-md-3">
<search-filter-view ...></search-filter-view>
</div>
<div class="col-md-9">
<search-result-view ...></search-result-view>
</div>
</div>
我的搜索过滤器 View 组件是这样的:
<script>
export default{
props:[...],
data(){
return{
...
}
},
methods:{
filterBySort: function (sort){
this.sort = sort
...
}
}
}
</script>
我的搜索结果 View 组件是这样的:
<script>
export default {
props:[...],
data() {
return {
...
}
},
methods: {
getVueItems: function(page) {
...
}
}
}
</script>
我想要将排序参数(filterBySort 方法,组件一)的值显示到 getVueItems 方法(组件二)
我该怎么做?
最佳答案
我将详细说明 Serge 提到的内容。在 Vue v1 中,组件可以向世界广播消息,而其他组件可以只是监听消息并对其采取行动。在 Vue2 中,它更加精致,更加明确。
您需要做的是创建一个单独的 Vue 实例作为对现有组件可见的信使或通信总线。示例(使用 ES5):
// create the messenger/bus instance in a scope visible to both components
var bus = new Vue();
// ...
// within your "result" component
bus.$emit('sort-param', 'some value');
// ...
// within your "filter" component
bus.$on('sort-param', function(sortParam) {
// ... do something with it ...
});
对于比简单的组件间通信更复杂的问题,应该研究 Vuex(Vue 相当于 React 的 Redux)。
关于javascript - 如何将变量值从组件一发送到组件二? (vue.js 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42277626/
我是一名优秀的程序员,十分优秀!