Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从api请求更新VueJs中的值,以便在不重新加载页面或单击操作的情况下更新按钮,或者是否有其他方法?_Javascript_Python_Api_Vue.js - Fatal编程技术网

Javascript 如何从api请求更新VueJs中的值,以便在不重新加载页面或单击操作的情况下更新按钮,或者是否有其他方法?

Javascript 如何从api请求更新VueJs中的值,以便在不重新加载页面或单击操作的情况下更新按钮,或者是否有其他方法?,javascript,python,api,vue.js,Javascript,Python,Api,Vue.js,我试图通过将whiletrue放入created中来解决这个问题,这样它就可以通过频繁地向flask发送请求来保持更新。是否有其他方法更新我的值 Vue: 。。。。flask只是一个基本会话返回响应: @app.route('/logstat', methods=["GET"]) def logstat(): '''Checking if user is authorised/session, returning username if so''' u

我试图通过将whiletrue放入created中来解决这个问题,这样它就可以通过频繁地向flask发送请求来保持更新。是否有其他方法更新我的值

Vue:

。。。。flask只是一个基本会话返回响应:

    @app.route('/logstat', methods=["GET"])
def logstat():
    '''Checking if user is authorised/session, returning username if so'''
    username = session.get("userid", None)
    return json.dumps(username)

@app.route("/logout")
def logout():
    '''Ending session/logout from account'''
    session.pop("userid", None)
    return 'OK'
在我的index.html中

<div v-if=(session) @click='logOut'>
                <p>Logged in as: {{session}} </p> <input type='submit' class='log_info_button pointer' name='submit'
                  value='LOG OUT' /> <i class='fa fa-sign-in' aria-hidden='true'></i>
              </div>
              <div v-else @click='logIn'> <input type='submit' class='log_info_button pointer' name='submit'
                  value='LOG INN' /> <i class='fa fa-sign-in' aria-hidden='true'></i></div>
            </div>
我要寻找的是一种方法,这样我的登录和注销按钮将根据“会话”状态进行更新,如果有sessionid,则会出现注销。如果为空,则登录。 可能还有另一个值可以帮助我代替sessionid吗


此.showAcc用于在导航栏中显示“我的帐户”链接。它还依赖于会话ID。

通常,这是由于在响应/错误回调中使用此关键字导致的,它引用的是函数本身,而不是Vue实例;你可以通过

将该值分配给另一个变量,并在回调中使用该变量 当实例被挂载时,还要尝试检查登录状态,除非会话很快过期,否则就不需要经常检查登录状态

    mounted: function(){
      this.checkSession();
    },
    methods:{
      checkSession: async function(){
         let _this = this;
         let response = await fetch("/logstat");
         if (response.status == 200) {
          let result = await response.json();
          _this.session = result;
          _this.showAcc = this.session != null ? "" : "none";
         }
        }
    }

如果您需要在后端或代理中刷新此信息,那么这肯定不是理想的方式

如果在获得会话信息后需要停止轮询后端,则可以执行以下操作:

... 数据:{…}, 创造{ 回程日志 }, 方法:{ 异步获取日志{ let response=wait fetch/logstat; 如果response.status==200{ 让result=wait response.json; 这个会话=结果; 如果this.session!=null{ this.showAcc=; }否则{ this.showAcc=无; this.fetchLog;//如果没有任何结果,则重复该操作 } } } } 这将消除烧瓶超时的需要


注:如果您愿意,您还可以为调用this.fetchLog设置一个小的超时,比如setTimeoutthis.fetchLog,500,持续半秒。不过,理想情况下,您还必须存储此超时id,并在卸载或销毁时使用它在退出时执行clearTimeout。

网站在没有用户交互的情况下更新当前登录状态是非常不寻常的,不管怎样,每当我要我的服务器更新我的页面时,我都会使用websocket。

这有什么用?您能否再解释一下“通常这是由于在响应/错误回调中使用此关键字导致的,它引用的是函数本身,而不是Vue实例”?我已经尝试过了,但它仍然没有更新我的按钮。我必须刷新。我的想法不是刷新,而是更新价值。我看到只有在单击或刷新页面时才调用/logstat。我想让它自己更新。但由于某些原因,它不是。在这种情况下,我可以推荐最后一种方法,再次调用它并超时。我也想到了websocket连接,但这有点复杂。非常感谢。这真的很有帮助。我在方法上也做了一些改变。在flask中使用sleep0.5代替setTimeout来处理/logstat请求可以吗?还是不是很常用?不管这是否有效,尽管应该,延迟后端响应而不是前端请求并不是此类情况的常用解决方案
    mounted: function(){
      this.checkSession();
    },
    methods:{
      checkSession: async function(){
         let _this = this;
         let response = await fetch("/logstat");
         if (response.status == 200) {
          let result = await response.json();
          _this.session = result;
          _this.showAcc = this.session != null ? "" : "none";
         }
        }
    }