如何从App.vue访问组件属性(How to access a component property from App.vue)
我使用vue-loader来帮助我安装vue和webpack我有一个名为App.vue的文件
在App.vue中,我添加了一个名为widget的组件。 如果我单击某个按钮,则会设置一个设置
btnClicked = true
的函数,从而显示小部件<widget v-show="btnClicked"></widget>
但我也希望该函数访问
widgetShowMe
,它是我组件中的属性。我希望在我的
App.vue
激活的功能也设置widgetShowMe = true
我试过这个但它没有用methods:{ btnClickedFunc () { this.btnClicked = true; Widget.widgetShowMe = true; } }
I used vue-loader to help me install vue and webpack I have a file called App.vue
In App.vue I added a component called widget. If I clicked some button there's a function that set the
btnClicked = true
hence the widget appears<widget v-show="btnClicked"></widget>
but I also want that function to access the
widgetShowMe
, it's a property in my component.I want the function activated in my
App.vue
to also setwidgetShowMe = true
I tried this but it didn't workmethods:{ btnClickedFunc () { this.btnClicked = true; Widget.widgetShowMe = true; } }
原文:https://stackoverflow.com/questions/42626810
最满意答案
我使用Angular事件来处理这样的事情 - 例如:
.controller('parentCtrl', function($scope,$rootScope) { $rootScope.$on('loading',function(e,_statusObj.loading) { $scope.loading = _statusObj.loading; if(!!_statusObj.msg) { alert(_statusObj.msg); } }); }) .controller('childCtrl', function($scope,$http) { $scope.myAjaxCall = function(_url,_data) { $scope.$emit('loading',{ loading: true}); $http.post(_url,_data).success(function(_response) { $scope.$emit('loading',{ loading: false }); }) .error(function(_error) { $scope.$emit('loading',{ loading : false, msg : _error.message }); }); } });
I managed to get the interceptor working. Apparently we CAN access the config file in all interceptor phases:
/****************************************** SETUP BUSY/ERROR/DATA HTTP INTERCEPTOR *******************************************/ .config(function($httpProvider){ $httpProvider.interceptors.push(function($q) { return { request : function(config) { if(config.ctrl){ config.ctrl.busy = true; config.ctrl.error = false; config.ctrl.data = undefined; } return config; }, response : function(response) { if(response.config && response.config.ctrl){ response.config.ctrl.busy = false; response.config.ctrl.data = response.data; } return response; }, responseError : function(response){ // note: maybe use a different error message for different kinds of responses? var error = response.status + " "+response.statusText+" - "+response.data; if(response.config && response.config.ctrl){ response.config.ctrl.busy = false; response.config.ctrl.error = error; } return $q.reject(error); } }; }); })
相关问答
更多-
我认为你需要使用承诺来回报错误。 将$q添加到您的拦截器工厂。 像这样 $provide.factory('MyHttpInterceptor', function ($q){ ... }) 然后得到responseError() function responseError(response) { if (response.status === 401) { $rootScope.$broadcast('unauthorized'); } return $q.reject(re ...
-
您需要在authInterceptor工厂方法中过滤掉所需的请求 ['/whatever/1', '/whatever/2', '/whatever/3'].forEach(function(value){ if (response.config.url.startsWith(value)) { // do something } }) return response; You need the filter out the requests you want in the authInte ...
-
正如手册所说, 请求:使用http配置对象调用拦截器。 该函数可以自由修改配置对象或创建新对象。 该函数需要直接返回配置对象,或者包含配置或新配置对象的promise。 所以它应该是: function requestInterceptor(req) { return $injector.get('tokenService').accessToken().then(function(res) { ... return req; }, functio ...
-
Angular http拦截器(Angular http interceptor)[2023-05-19]
你的代码流是这样的: 0)AngularJS定义$httpProvider ; 1)你定义loginService ,它取决于$httpProvider注入$http ; 2)你定义一个依赖于loginService的HTTP拦截器并改变$http工作方式; 3)您定义注入$http其他服务。 看看这个函数,任何AngularJS提供者必须提供的$get方法。 每次你的服务将$http作为依赖项注入并返回 $http时调用它。 现在,如果你回到第396行,你会看到在调用$get时会构建一个reversedI ... -
您在导入的axios实例上调用拦截器,但它需要在您创建的实例上。 无论如何,调用window.axios = axios.create()是非常糟糕的风格,你应该不惜一切代价避免它。 如果您希望它全局可用,则应将其绑定到Vue Prototype。 更好的方法是将其移出另一个模块: const instance = axios.create({ baseURL: 'http://localhost:8080', timeout: 10000, params: {} // do not ...
-
有类似的问题。 我的代码有点不同,但想法是一样的,试试这个 var deferred = $q.defer(); authService.refreshToken().then(function () { $http(errorResponse.config).then(deferred.resolve, deferred.reject); }, function () { authService.logOut().then(deferred.reject); ...
-
角请求忙拦截器(angular request busy interceptor)[2023-04-07]
我使用Angular事件来处理这样的事情 - 例如: .controller('parentCtrl', function($scope,$rootScope) { $rootScope.$on('loading',function(e,_statusObj.loading) { $scope.loading = _statusObj.loading; if(!!_statusObj.msg) { ale ... -
也许你可以试试这个。 public String intercept(ActionInvocation invocation) throws Exception { final ActionContext context = invocation.getInvocationContext(); Map
parameters = (Map )context.get(ActionContext.PARAMETERS); Map< ... -
当您最初有一个经过身份验证的请求时,会有一个OPTIONS类型的请求在调用的预检期间完成。 这是服务器与服务器的一种握手,弄清楚它是否接受这种类型的请求。 如果一切顺利,它将执行您最初做的实际GET,POST等请求。 我相信那就是你正在经历的。 编辑 您的问题可能以您设置标题的方式存在。 尝试以下操作 request = request.clone({ headers: headersConfig }); When you have an authenticated request i ...
-
为什么不在路由更改事件$on观察$on并将当前/以前的路径绑定到$window或全局$scope变量? 我还没有阅读上面的所有代码,但这就是我在应用程序中处理位置的方法 : $scope.$on('$routeChangeSuccess', function(evt, current, previous){ var routeData = {}; //prop: uriSegment //desc: get all uri segments for current location ...