beego+vue父子組件通訊(父子頁面傳值、父子組件傳值、父子路由傳值)

場景:有head和foot,爲父組件
側欄tree爲子組件
點擊tree,右側孫組件根據點擊tree的id,來更改表格內容。
javascript

  1. 首先是父子(本例中是子組件與孫組件)通訊,目前是父傳到子,暫時尚未子傳到父。
  • vue中路由設置以下:
export default new Router({
  routes: [
    {
      path: '/',
      redirect: {
        name: '首頁'
      },
      component: cmshome,//父組件
      children: [  
        { path: '/', component: cmsindex, name: 'cmsindex' },    
        { path: '/projects', component: projects, name: 'projects' },
        { path: '/project', component: projecthome,name: 'projecthome',//子組件
          children: [
            {path: '/project', component: project, name: 'project'}//孫組件
          ]
        },
      ]
    },
  • 子組件中設置:treeId,由於tree-id就是treeId,這個子組件中的treeId值將會被孫組件中props獲取到。
<router-view :tree-id="treeId"></router-view>
  • 孫組件:
props:['treeId'],
  1. 其次是點擊tree,孫組件得到了id後,用watch來監視數據變化,當發現treeid變化時,就調用方法來請求服務端,得到表格中的json數據。
watch: {    // 若是 question 發生改變,這個函數就會運行
        treeId: function (newQuestion) {
          this.projproducts(this.currentPage);
        }
      },
相關文章
相關標籤/搜索