Vue.js 仅从UI触发的Vue监视字段
“我的页面”包含一个包含三个字段的组件,表示用户选择:Vue.js 仅从UI触发的Vue监视字段,vue.js,vuejs2,vue-component,Vue.js,Vuejs2,Vue Component,“我的页面”包含一个包含三个字段的组件,表示用户选择: 状态(活动、禁用) 位置(A楼、B楼) 名称(测试用户) 这三个字段相互连接,并且所有字段都在列表中,因此如果用户希望选择其他用户,例如: 首先选择活动状态(选择活动用户的所有位置) 然后选择建筑(选择在建筑中工作的所有用户) 最后选择测试用户 在组件的解决方案中,我创建了3个监视字段(selectedUserState、selectedLocation、selectedUserFullName),如果其中一个字段发生更改,则刷新其
- 状态(活动、禁用)
- 位置(A楼、B楼)
- 名称(测试用户)
- 首先选择活动状态(选择活动用户的所有位置)
- 然后选择建筑(选择在建筑中工作的所有用户)
- 最后选择测试用户
var filter = Vue.component('UserFilter', {
template: '#userfilter-template',
data() {
return {
userFilter: {},
selectedUserState: "",
selectedLocation: "",
selectedUserFullName: ""
}
},
watch: {
selectedUserState: function (newValue, oldValue) {
var searchContext = {};
searchContext.State = this.selectedUserState.Key;
this.refreshFilter(searchContext);
},
selectedLocation: function (newValue, oldValue) {
var searchContext = {};
searchContext.State = this.selectedUserState.Key;
if (this.selectedLocation) {
searchContext.LocationID = this.selectedLocation.Key;
}
this.refreshFilter(searchContext);
},
selectedUserFullName: function (newValue, oldValue) {
var searchContext = {};
searchContext.State = this.selectedUserState.Key;
if (this.selectedLocation) {
searchContext.LocationID = this.selectedLocation.Key;
}
if (this.selectedUserFullName) {
searchContext.FullName = this.selectedUserFullName;
}
this.refreshFilter(searchContext);
}
},
methods: {
refreshFilter(searchContext) {
postData(url, { searchContext: searchContext })
.then(response => {
if (response) {
this.userFilter = response.data.result;
if (this.selectedUserFullName !== this.userFilter.selectedUserFullName) {
this.selectedUserFullName = this.userFilter.selectedUserFullName;
}
if (this.selectedLocation !== this.userFilter.selectedLocation) {
this.selectedLocation = this.userFilter.selectedLocation;
}
if (this.selectedUserState !== this.userFilter.selectedUserState) {
this.selectedUserState = this.userFilter.selectedUserState;
}
}
});
}
},
mounted() {
this.refreshFilter(null);
}});
问题是,我只是想在用户在UI中更改这些监视字段时触发它们,但在设置新值时,它会在刷新方法中触发,并触发不必要的刷新
我试图找到一个移除触发器的unwatch方法和一个绑定触发器的watch方法,但没有成功
提前感谢您的帮助。一个简单的解决方案是,即使在您的UI上也要进行更改(我想是下拉菜单吧?) 这将触发基本上与手表功能相同的功能