【问题标题】:How to hide progress-linear bar after populating the array填充数组后如何隐藏进度条
【发布时间】:2018-07-16 10:12:32
【问题描述】:

隐藏进度线性组件的最佳方法是什么?我是否应该在等待几秒钟后实现它:

axios.get(this.endpoint)
        .then(response => this.data = response.data)
        .catch(error => console.log(error.response.data));

我想在填充数组后隐藏进度条。

        <v-progress-linear style="margin : 0 auto ;" :indeterminate="isLoading" v-show="isLoading = false"></v-progress-linear>

我在这里的目的是展示一些 x 秒,这样我就可以展示它一些很酷的效果

【问题讨论】:

  • 所以您不是在问如何做,而是在问最好的方法是什么?你能澄清一下吗?因为您似乎可以通过将isLoading 设置为false 来隐藏它。何时设置取决于您(取决于用例)。
  • @Traxo,我问你,因为你在这些事情上有更好的方法:) 是的,我忘了复制 isLoading = false
  • 但我不太明白这里的问题是什么。因此,如果可能,我恳请您澄清一下。如果您想在填充数组后的某个时间隐藏它,那么您可以在 .then 中使用 setTimeout 例如。如果您询问要采用哪种方法,那么UX 可能是更好的提问地点(但您可能想包含更多信息)?除非我误会了什么。也许其他人也可以确认。
  • 你错过了括号:axios.get(this.endpoint) .then(response =&gt; { this.parcels = response.data; setTimeout(() =&gt; { this.isLoading = false; }, 5000); } );
  • @Traxo,您的代码不起作用,我收到错误消息,“isLoading”未在实例上定义,但在渲染期间被引用。请创建一个 jsfiddle 示例

标签: vuetify.js


【解决方案1】:

Codepen

模板:

<v-progress-linear v-show="isLoading"></v-progress-linear>

脚本:

data: () => ({
  isLoading: true
}),
mounted() {
  setTimeout(() => {
    this.isLoading = false;
  }, 5000); // toggle after 5 seconds
}

所以基本上只需在组件上设置v-show 并在您想要隐藏/显示组件时切换它。在 API 调用后将其设置为 false(例如在 then 方法回调中)。

我只使用了 5 秒 (5000ms) 超时作为示例,以便您可以注意到变化。当然,您可以根据需要进行更改。

在 API 响应后等待 5 秒的示例如下所示:

axios.get(this.endpoint).then(response => {
  this.parcels = response.data;
  setTimeout(() => {
    this.isLoading = false;
  }, 5000);
});

【讨论】:

    【解决方案2】:

    只需分享我的工作代码:

    模板:

    :loading="progress_bar_loading"    <!--- ----- progressing bar indicator --------- true: show ---------- false: hide  --->
    

    你需要设置 :loading = "any_variable",

    稍后在 js 中,您会将其重置为 true/false 作为显示/隐藏进度条。

    上下文在这里:

      <v-data-table
                :headers="headers"
                :items="user"
    
                :loading="progress_bar_loading"    <!--- ----- progressing bar indicator --------- true: show ---------- false: hide  --->
    
    
                :search="search"
               <!---  hide-actions  --->      <!--- ----- hide/show  pagination -------------  --->
                class="elevation-1"
    
    
              >
    
    
    
                    <v-progress-linear slot="progress"  indeterminate></v-progress-linear>
    
                                    <template slot="items" slot-scope="props" >
                                      <td>{{ props.item.full_name }}</td>
                                      <td class="text-xs-left">{{ props.item.name }}</td>
                                      <td class="text-xs-left">{{ props.item.password }}</td>
                                      <td class="text-xs-left">{{ props.item.agency }}</td>
                                      <td class="text-xs-left">{{ props.item.level }}</td>
    

    js:

    在 ajax 或 fetch 之前显示进度条:

          // show v-data-table progress bar
                 self.progress_bar_loading = true
    

    ajax fetch 回调成功后,隐藏进度条:

           // hide v-data-table progress bar
                                    self.progress_bar_loading = false
    

    注意:你必须使用 self.不要使用这个。因为范围问题。

    上下文在这里:

    new Vue({
       el: '#app',
           data: () => ({
    
    
    
      created () {
    
     // Use creation hooks if you need to set things up in your component both during client rendering and server rendering. 
     // You will not have access to the DOM or the target mounting element (this.$el) inside of creation hooks 
    
    this.initialize()
       },
    
    
      mounted () {
    
    //Use mounted if You need to access or modify the DOM of your component immediately before or after the initial render.
    // Do not use mounted if You need to fetch some data for your component on initialization. Use created (or created + activated for keep-alive components) for this instead, especially if you need that data during server-side rendering.
    
    //this.initialize()
       },
    
    
    
    
       methods: {
    
    
    initialize () {
    
    
    
                     var getUser_url = url + 'cfc/sw.cfc?method=getUser&returnformat=json&queryformat=struct';
    
                    console.log(getUser_url )
    
                /*
                    You can use a plethora of options for doing Ajax calls such as Axios, vue-resource or better yet the browser's built in fetch API in modern browsers. 
                    You can also use jQuery via $.ajax() API, which simply wraps the XHR object in a simple to use method call 
                    but it's not recommended to include the whole jQuery library for the sake of using one method.
                    https://www.techiediaries.com/vuejs-ajax-http/
    
                    http://updates.html5rocks.com/2015/03/introduction-to-fetch
                    The Fetch API provides a JavaScript interface for accessing and manipulating parts of the HTTP pipeline, such as requests and responses. 
                    It also provides a global fetch() method that provides an easy, logical way to fetch resources asynchronously across the network.
    
                */
    
    
                    //   **********  must use self = this ************** 
                    // this reference vue-app.  must pass it to self, then pass into callback function (success call back)
                    var self = this;  
    
    
    
    
                    // show v-data-table progress bar
                     self.progress_bar_loading = true
    
    
    
    
                    fetch(getUser_url)
                            .then(function (response) 
                                   {
    
                                         // if js error here, likely it is coldfusion server output error message instead of valid json 
                                         // so check coldfusion server response.
                                           return response.json();
    
                                    })
    
                            .then(function (result) {
    
    
    
    
                                     //------------------------ properties to lowercase ----------------------
    
                                     /* 
                                         result = [{"FULL_NAME":"xXx"}, {}, {}....... {}]
                                         result is upper case, must convert all properties to lowercase, 
                                         result = [{"full_name":"xXx"}, {}, {}....... {}]
                                         however, the value like password or number MUST remain no change. 
                                     */
    
                                     // result = [{}, {}, {}....... {}]
                                        var result_lower_properties= [];
    
                                        var arrayLength = result.length;
    
                                        for (var i = 0; i < arrayLength; i++) {
    
                                            var obj = result[i];
                                            var obj_lower_properties = {};
    
                                            for (var prop in obj) {
    
                                                              //console.log(prop.toLowerCase())
                                                              //console.log(obj[prop])
    
                                                              obj_lower_properties[prop.toLowerCase()] = obj[prop]
                                            }// for
    
                                            result_lower_properties.push(obj_lower_properties)
    
                                        }// for
    
                                      //----------  ENd -------------- properties to lowercase ----------------------
    
    
    
    
    
                                     // must use self.user,  do not use this.user, 
                                     // because here, this's scope is just the function (result).   
                                     // we need this reference to vue-app, 
                                     self.user = result_lower_properties  // [{}, {}, {}]  
    
    
                                     // hide v-data-table progress bar
                                    self.progress_bar_loading = false
    
        }); // fetch(){}
    
    
       console.log(JSON.stringify(this.user));
    
    
    
    
    
    },  // initialize {}
    

    完整的源代码在这里:

    template

    js

    【讨论】:

      【解决方案3】:

      您可以使用 watcher 继续监视您的数据数组,以便在填充后您可以隐藏进度条。

      模板:

      <v-progress-linear v-show="isLoading"></v-progress-linear>
      

      脚本:

      data: () => ({
         isLoading: true,
         data:[]
      }),
      watch() {
         data(){
            this.isLoading = false
         }
      }
      

      在 Codepen 上查看演示

      https://codepen.io/Agusto/pen/XoqLvb

      【讨论】:

        【解决方案4】:

        这一切都可以通过您的&lt;v-data-table&gt; 中的另一个模板来完成。您可以将&lt;v-progress-linear&gt; 包装在&lt;template slot="no-data"&gt; 中。然后,当您的 items 数组被填充时,无数据模板以及其中的所有内容都会被隐藏。

        模板:

        <template slot='no-data'>
            <v-progress-linear slot='progress' indeterminate></v-progress-linear>
        </template>
        

        如果您想变得更花哨,您可以在模板中的&lt;v-progress-linear&gt; 下添加一个&lt;v-alert&gt;,以便让您的用户知道他们在等待什么。 :-)

        <v-alert :value="true" color="info" icon="warning" class="mb-4">Loading data...</v-alert>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-10
          相关资源
          最近更新 更多