Vuejs2 创建Vue组件的原因是什么?

Vuejs2 创建Vue组件的原因是什么?,vuejs2,vue-component,vue-router,Vuejs2,Vue Component,Vue Router,我已经基于CMS中的服务器页面返回的数据实现了组件路由。摘要-在路由更改中,路径被发送到服务器,服务器返回该模板的模板名称和数据。模板名称映射到组件名称,然后组件名称使用数据。我看到的问题是,在更改布线后,在创建和显示目标组件时,原始组件(源布线)也会创建两次 下面是从服务器获取数据并使用它设置新路由的代码 router.beforeEach((to, from, next) => { if (!app) { next() return } // get pag

我已经基于CMS中的服务器页面返回的数据实现了组件路由。摘要-在路由更改中,路径被发送到服务器,服务器返回该模板的模板名称和数据。模板名称映射到组件名称,然后组件名称使用数据。我看到的问题是,在更改布线后,在创建和显示目标组件时,原始组件(源布线)也会创建两次

下面是从服务器获取数据并使用它设置新路由的代码

router.beforeEach((to, from, next) => {
  if (!app) {
    next()
    return
  }
  // get page data from the server.
  app.$store.dispatch('getPageData', to.fullPath).then(r => {
    // check whether vue-router knows this route
    // if not then map it to the right component
    if (to.matched.length === 0) {
      let component = mapTemplate(r.data.page.template)
      // add the route (undocumented but intentionally released)
      router.addRoutes([{path: to.path, component}])
      // change to the same route now that vue-router knows it
      next(to.fullPath)
      return
    }
    next()
  })
})
现在是控制台日志(节略)。日志中捕获的序列是:

  • 加载使用模板
    p-home.vue
    的“/”
  • 使用菜单导航至/成衣/使用组件
    p-rtw-products
  • 粗体行显示
    p-home
    生命周期挂钩。斜线显示路由完成时的
    vue路由器
    。显示“设置路由”的行显示添加到路由器的新路由

    p-home.vue?1e58:54 p-home created p-home.vue?1e58:54 p-home mounted lmd-app.vue?70d9:48 fetchdata for / index.js?3672:62 router.beforeEach() / => /ready-to-wear/ p-home.vue?1e58:54 p-home destroyed p-home.vue?1e58:54 p-home created p-home.vue?1e58:54 p-home mounted index.js?3672:73 beforeEach() setting route /ready-to-wear/ target: PRtwProducts index.js?3672:62 router.beforeEach() / => /ready-to-wear/ p-home.vue?1e58:54 p-home destroyed p-home.vue?1e58:54 p-home created p-home.vue?1e58:54 p-home mounted index.js?3672:77 router.afterEach() p-rtw-products.vue?5a57:22 p-rtw-products created p-home.vue?1e58:54 p-home destroyed p-rtw-products.vue?5a57:25 p-rtw-products mounted p-home.vue?1e58:54创建了p-home p-home.vue?1e58:54 p-home安装 lmd app.vue?70d9:48获取的数据/ index.js?3672:62 router.beforeach()/=>/成衣/ p-home.vue?1e58:54 p-home被摧毁 p-home.vue?1e58:54创建了p-home p-home.vue?1e58:54 p-home安装 index.js?3672:73 beforeach()设置路线/成衣/目标:PRtwProducts index.js?3672:62 router.beforeach()/=>/成衣/ p-home.vue?1e58:54 p-home被摧毁 p-home.vue?1e58:54创建了p-home p-home.vue?1e58:54 p-home安装 index.js?3672:77路由器.afterEach() p-rtw-products.vue?5a57:22已创建p-rtw-products p-home.vue?1e58:54 p-home被摧毁 p-rtw-产品。vue?5a57:25 p-rtw-产品安装 好的,谢谢你走到这一步。我希望为p-home创建一个销毁序列,但是有三个。我甚至可以想象,因为它是路由的原始来源,所以创建它已经排队了(尽管我真的不相信它)


    有人知道它为什么会这样做吗?

    有几点需要注意:最终的结果是使用正确的组件获得正确的数据,但我想了解无法解释的行为(除非我记录了生命周期挂钩,否则我不会注意到)。并且对上面的代码和日志进行了编辑,以使它们更加集中。如果有任何错误,请告诉我。您的组件是独立的还是“p-rtw-products”不知何故位于“p-home.vue”中?换句话说,“路由器视图”被称为“p-home”内部还是外部包装内部?其结构是每个
    p-
    页面都使用一个通用页面(
    c-standard-page
    c-standard-page
    有一个页眉、页脚和一个用于
    p-component
    s'特定内容的插槽。每个
    p-
    c-
    组件都实现为一个单独的
    .vue
    文件。短版本,没有
    p-rtw-products
    p-home.vue
    中(除非我遗漏了一些明显的东西)。