te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Is Vue's 'destroyed' method called on page refresh? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is Vue's 'destroyed' method called on page refresh? - Stack Overflow

programmeradmin4浏览0评论

I am wondering if refreshing a page that runs a Vue app will trigger the Vue's .destroyed callback.

From what I observed in a Vue app that contains these simple lifecycle callbacks:

created() {
  console.log(' created');
},
destroyed() {
  console.log('destroyed');
}

only 'created' is logged (not 'destroyed'). How can I check if the .destroyed callback has been executed?

I am wondering if refreshing a page that runs a Vue app will trigger the Vue's .destroyed callback.

From what I observed in a Vue app that contains these simple lifecycle callbacks:

created() {
  console.log(' created');
},
destroyed() {
  console.log('destroyed');
}

only 'created' is logged (not 'destroyed'). How can I check if the .destroyed callback has been executed?

Share Improve this question edited Jul 14, 2022 at 1:48 tony19 138k23 gold badges277 silver badges346 bronze badges asked May 10, 2019 at 10:47 user7693832user7693832 6,84925 gold badges77 silver badges129 bronze badges 1
  • 4 FYI for others finding this in the search engines. destroyed() has now (Vue 3) been renamed to unmounted() – redfox05 Commented Mar 12, 2021 at 17:23
Add a ment  | 

2 Answers 2

Reset to default 8

I found the similar question and answer to it on stackoverflow

Do something before reload or close in vue.js

He/she basically explains that nothing is destroyed on page reload, you need to define

window.onbeforeunload = function(){
  return "Are you sure you want to close the window?";
}

If you want to do something before a page refresh

As your question was

Is Vue's 'destroyed' method called on page refresh?

No, destroyed method called if your ponent's controller lost or you manually destroy, above example is for manually destroy.

I have found very good example in vuejs forum which uses externally this.$destroy() method.

new Vue({
  el: '#app',
  data() {
    return {
      value: 'will work until destroy'
    };
  },
  methods: {
    destroy() {
      this.$destroy();
    }
  },
  beforeDestroy() {
    console.log('Main Vue destroyed')
  }
})

var tmp = Vue.extend({
  template: `
  	<div>
      <span>{{ value }}</span>
      <input v-model="value" />
    </div>
  `,
  data() {
    return {
      value: 'always bind and work'
    };
  },
  beforeDestroy() {
    console.log('Mounted destroyed')
  }
});

new tmp().$mount('#mount-point');
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.6.10/vue.js"></script>
<div id="app">
  {{ value }}
  <input v-model="value" />
  <div id="mount-point"></div>
  <button @click="destroy()">Destroy</button>
</div>

Reference

Another example. If ponent's control lost or removed then destroy method will be called of that ponent's

Vue.ponent('p1', {
  template: '<div>A custom ponent1!</div>',
  destroyed(){
    console.log('p1 destroyed');
  }
})

Vue.ponent('p2', {
  template: '<div>A custom ponent2!</div>',
  destroyed(){
    console.log('p2 destroyed');
  }
})

new Vue({
  el: '#app',
  data() {
    return {
      value: 1
    };
  },
  methods: {
  },
  beforeDestroy() {
    console.log('Main Vue destroyed')
  }
})
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.6.10/vue.js"></script>
<div id="app">
  <select v-model="value">
    <option value="1">p1</option>
    <option value="2">p2</option>
  </select>
  <p1 v-if="value==1"></p1>
   <p2 v-if="value==2"></p2>
  <button @click="destroy()">Destroy</button>
</div>

发布评论

评论列表(0)

  1. 暂无评论