简体   繁体   English

Vue JS 2:在 beforeCreate 之后停止生命周期

[英]Vue JS 2: Stop lifecycle after beforeCreate

Is there a way to stop rendering and execution of created() and mounted() in the beforeCreate method?有没有办法在 beforeCreate 方法中停止渲染和执行 created() 和 mounted() ? Just calling $destroy() on the component will fire after created.只需在组件上调用 $destroy() 就会在创建后触发。

Vue.js 生命周期

No. There are other ways to prevent a component from rendering不,还有其他方法可以阻止组件渲染

v-if

Use v-if in the parent to conditionally render.在父级中使用v-if有条件地渲染。

<child v-if="shouldRender"></child>
data: () => ({
  shouldRender: false
})

beforeRouteEnter

If the component is a router page, you can prevent it from being created with the beforeRouteEnter navigation guard.如果该组件是一个路由页面,您可以使用beforeRouteEnter导航守卫来阻止它被创建。

beforeRouteEnter (to, from, next) {
  if (something) {
    // Don't render the component, go somewhere else
    next({ path: '/somewhere' });  
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM